TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Correctly implementing a spinlock in Modern C++

115 pointsby symisc_develalmost 4 years ago

13 comments

malkiaalmost 4 years ago
Spinlocks are great way of telling the kernel that you believe in small governments, and no regulations. I mean really, why should the kernel meddle in your user business?
评论 #27638277 未加载
评论 #27639445 未加载
评论 #27639078 未加载
sillysaurusxalmost 4 years ago
One neat thing is, you don&#x27;t actually need a spinlock if you&#x27;re using a ringbuffer. You can use atomic increment on a counter to claim a slot, then write to that slot.<p>It can be between two to three orders of magnitude higher throughput. Equally important, lower latency.<p>(Throughput tends to be a consequence of low latency, but not always.)<p>I&#x27;m not saying this should be the norm, though. You probably don&#x27;t need this design. But when you do, e.g. processing millions of stock market messages, there&#x27;s no substitute.<p>EDIT: I love hearing about the designs and questions, but it was probably a mistake for me not to be explicit. Sorry! The thing I&#x27;m referring to is LMAX Disruptor pattern: <a href="https:&#x2F;&#x2F;lmax-exchange.github.io&#x2F;disruptor&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lmax-exchange.github.io&#x2F;disruptor&#x2F;</a><p>I learned about it in 2011-ish, and it deeply changed my perspective on high speed designs.
评论 #27638282 未加载
评论 #27638531 未加载
评论 #27638419 未加载
评论 #27679720 未加载
评论 #27639923 未加载
评论 #27638236 未加载
tialaramexalmost 4 years ago
Notice that this code probably has a bug. [Edited to add: spacechild1 says it doesn&#x27;t, and I now believe them]<p>It keeps using exchange() which swaps the old value in memory for your value and give back the old value, but it sets std::memory_order_acquire with the author apparently thinking that since this wants to <i>acquire</i> a lock this is enough.<p>But it isn&#x27;t. The exchange() call is <i>two</i> memory operations, it&#x27;s a load <i>and</i> a store, and so what you wanted here was Acquire <i>and</i> Release semantics ie. memory_order::acq_rel<p>What has been written is effectively Relaxed semantics for the store, and C++ doesn&#x27;t do a great job of explaining that to programmers.
评论 #27640201 未加载
评论 #27640072 未加载
raphlinusalmost 4 years ago
The debate about whether further reordering is possible is an interesting one. I think it&#x27;s evidence that it&#x27;s basically not possible for humans to reason about the memory model. The best way to resolve it is to use an automated checking tool based on a formal model. Good choices are CDSChecker[1] or the newer CppMem[2].<p>[1]: <a href="http:&#x2F;&#x2F;plrg.ics.uci.edu&#x2F;software_page&#x2F;42-2&#x2F;" rel="nofollow">http:&#x2F;&#x2F;plrg.ics.uci.edu&#x2F;software_page&#x2F;42-2&#x2F;</a><p>[2]: <a href="http:&#x2F;&#x2F;svr-pes20-cppmem.cl.cam.ac.uk&#x2F;cppmem&#x2F;help.html" rel="nofollow">http:&#x2F;&#x2F;svr-pes20-cppmem.cl.cam.ac.uk&#x2F;cppmem&#x2F;help.html</a>
samsquirealmost 4 years ago
Multithreading sure is complicated.<p>I&#x27;m currently playing with multithreading right now. I&#x27;m implementing snapshot isolation multiversion concurrency control.<p>In theory you can avoid locks (except for data structure locks) by creating a copy of the data you want to write and detect conflicts at read and commit time. Set the read timestamp of a piece of data to the transaction timestamp (timestamps are just monotonically increasing numbers) that reads it. If someone with a higher transaction timestamp comes along, they abort and restart because someone got in before them.<p>At the moment I have something that mostly works but occasionally executes a duplicate. I&#x27;m trying to eradicate the last source of bugs but as with anything parallel, it&#x27;s complicated due to the interleavings.<p>My test case is to spin up 100 threads, with each thread trying to increment a number. The end numbers should be 101 and 102. If there was a data race, then the numbers will be lower.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;multiversion-concurrency-control" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;multiversion-concurrency-contro...</a>
评论 #27638438 未加载
评论 #27639937 未加载
jeffbeealmost 4 years ago
For a real battle-tested combination of spinlock and futex, see <a href="https:&#x2F;&#x2F;github.com&#x2F;abseil&#x2F;abseil-cpp&#x2F;blob&#x2F;master&#x2F;absl&#x2F;synchronization&#x2F;mutex.cc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;abseil&#x2F;abseil-cpp&#x2F;blob&#x2F;master&#x2F;absl&#x2F;synchr...</a>
评论 #27639590 未加载
评论 #27680473 未加载
AnanasAttackalmost 4 years ago
Correctly using a spinlock: Never when preemption is enabled
评论 #27638086 未加载
评论 #27680855 未加载
inshadowsalmost 4 years ago
Isn&#x27;t it nonsense to do this in user-space? Your thread can get preempted at any moment.
评论 #27638939 未加载
评论 #27639210 未加载
评论 #27680275 未加载
评论 #27640568 未加载
评论 #27638890 未加载
RcouF1uZ4gsCalmost 4 years ago
I think it may become even easier with C++20 and the addition of wait&#x2F;notify to std::atomic<p><a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;atomic&#x2F;atomic&#x2F;wait" rel="nofollow">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;atomic&#x2F;atomic&#x2F;wait</a>
评论 #27638576 未加载
gpderettaalmost 4 years ago
Regarding moving loads and stores into acq&#x2F;rel critical sections, this is possible, well studied and documented; it is known roach motel reordering since the Java MM standardisation, and generally considered safe.<p>I never thought whether it could lead to deadlocks or not and it is an interesting question. I assume there musy ve some formal proof against it but I would have to think about it (my first hunch is that if the reordering could cause a deadlock, then the cose wasn&#x27;t safe in any case, like not acquiring locks in a consistent order).
gentleman11almost 4 years ago
What is your favourite resource for studying multithreaded programming?
评论 #27639746 未加载
MichaEileralmost 4 years ago
I&#x27;m surprised nobody has mentioned Linus&#x27;s rant about spinlocks in userspace: <a href="https:&#x2F;&#x2F;www.realworldtech.com&#x2F;forum&#x2F;?threadid=189711&amp;curpostid=189752" rel="nofollow">https:&#x2F;&#x2F;www.realworldtech.com&#x2F;forum&#x2F;?threadid=189711&amp;curpost...</a>
评论 #27680355 未加载
评论 #27645205 未加载
ameliusalmost 4 years ago
How would you test this?
评论 #27680379 未加载