TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

An Introduction to Lock-Free Programming

128 点作者 preshing将近 13 年前

6 条评论

haberman将近 13 年前
Nice article, though I think that any intro-level material on lock-free programming should always include a "don't try this at home for anything important" warning. Until you have some experience with this stuff you will almost certainly make mistakes, but these mistakes might only manifest themselves as crashes in extremely rare circumstances.<p>I wrote my first lock-free code in 2004 based on reading some papers by Maged Michael from IBM. I wrote a lock-free FIFO in PowerPC assembly, and was convinced it was safe and robust. When I emailed Maged about it, he pointed out that if a thread was suspended on one specific instruction and some specific memory was unmapped before it could run again, the program could crash. I was amazed; I had thought hard about this algorithm, but had completely missed that possibility.<p>Some other specific notes about the article:<p>&#62; Basically, if some part of your program satisfies the following conditions, then that part can rightfully be considered lock-free.<p>The are actually several levels of lock-freedom defined in the literature: lock-freedom, wait-freedom, and obstruction-freedom. For more info see: <a href="http://en.wikipedia.org/wiki/Non-blocking_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Non-blocking_algorithm</a><p>&#62; Processors such as PowerPC and ARM expose load-link/store-conditional instructions, which effectively allow you to implement your own RMW primitive at a low level, though this is not often done.<p>One benefit of load-linked/store-conditional (often abbreviated LL/SC) is that it avoids the ABA problem (<a href="http://en.wikipedia.org/wiki/ABA_problem" rel="nofollow">http://en.wikipedia.org/wiki/ABA_problem</a>). In practice this doesn't matter that much since x86 doesn't support LL/SC, but I just think it's an interesting factoid to know.<p>&#62; For instance, PowerPC and ARM processors can change the order of memory stores relative to the instructions themselves, but the x86/64 family of processors from Intel and AMD cannot.<p>(I've edited my reply here since my original assertion was incorrect). It's true that x86/64 won't reorder stores (see <a href="http://en.wikipedia.org/wiki/Memory_ordering" rel="nofollow">http://en.wikipedia.org/wiki/Memory_ordering</a> for details) but it <i>will</i> reorder loads, so memory barriers are still required in some situations. However I believe that the atomic instructions ("lock cmpxchg", and "lock xadd") imply full barriers on x86.
评论 #4101385 未加载
评论 #4101558 未加载
tptacek将近 13 年前
The idea that made this click for me 10 years or so ago was that distributed systems express concurrent processes but can't reasonably use locks; model your threads/processes/whatever as if they were nodes in a distributed system, and use distributed system techniques to ensure things converge.
评论 #4100818 未加载
评论 #4103467 未加载
rdtsc将近 13 年前
There is also a niche research area of lock-free data structures and algorithms.<p>On a more practical side, check out <a href="http://www.liblfds.org/" rel="nofollow">http://www.liblfds.org/</a> -- it is a library of lock free data structures in C. (I am not the author). I have successfully used this library in some realtime projects.
评论 #4101456 未加载
sbahra将近 13 年前
A while back I linked to the following: <a href="http://concurrencykit.org/presentations/lockfree_introduction/#/" rel="nofollow">http://concurrencykit.org/presentations/lockfree_introductio...</a><p>Please make sure to navigate all the way down, before navigating to the right. Press the space bar to see the full layout of the presentation.
rossjudson将近 13 年前
Very good overview. I've built lock-free hash maps, and this article covers the areas you need to be aware of. I'd also suggest looking up false sharing, and also making sure that you really understand exactly how the memory barriers are operating.
cheatercheater将近 13 年前
Make sure to check out pi calculus for a different model of concurrency without locks: <a href="http://mainisusuallyafunction.blogspot.com/2011/09/lambda-to-pi.html" rel="nofollow">http://mainisusuallyafunction.blogspot.com/2011/09/lambda-to...</a>