Lock-Free Code: A False Sense of Security
By Herb Sutter, September 08, 2008
Writing lock-free code can confound anyone—even expert programmers, as Herb shows this month.
Herb is a software development consultant, a software architect at Microsoft, and chair of the ISO C++ Standards committee. He can be contacted at www.gotw.ca.
Given that lock-based synchronization has serious problems [1], it can be tempting to think lock-free code must be the answer. Sometimes that is true. In particular, it's useful to have libraries provide hash tables and other handy types whose implementations are internally synchronized using lock-free techniques, such as Java's ConcurrentHashMap, so that we can use those types safely from multiple threads without external synchronization and without having to understand the subtle lock-free implementation details.
But replacing locks wholesale by writing your own lock-free code is not the answer. Lock-free code has two major drawbacks. First, it's not broadly useful for solving typical problems—lots of basic data structures, even doubly linked lists, still have no known lock-free implementations. Coming up with a new or improved lock-free data structure will still earn you at least a published paper in a refereed journal, and sometimes a degree.
Second, it's hard even for experts. It's easy to write lock-free code that appears to work, but it's very difficult to write lock-free code that is correct and performs well. Even good magazines and refereed journals have published a substantial amount of lock-free code that was actually broken in subtle ways and needed correction.
To illustrate, let's dissect some peer-reviewed lock-free code that was published here in DDJ just two months ago [2]. The author, Petru Marginean, has graciously allowed me to dissect it here so that we can see what's wrong and why, what lessons we should learn, and how to write the code correctly. That someone as knowledgable as Petru, who has published many good and solid articles, can get this stuff wrong should be warning enough that lock-free coding requires great care.
Mirantis Releases Free Developer Edition
Applause Launches Mobile Beta Management
Jolt Awards 2014: The Best Utilities
Jolt Awards: Coding Tools
First-Class Functions in Java 8
FutureCast: Workplace of the Future
A Gentle Introduction to OpenCL
C/C++ Recent Articles
The C++14 Standard: What You Need to Know
State Machine Design in C++
Lambdas in C++11
A Lightweight Logger for C++
C++11's async Template
It's Hard To Compare Floating-Point Numbers
Auto Types and Range-Based For Statements in C++11