If, on the other hand, you really want to understand the memory model, you have your work cut out for you. Under no conditions should you start with the C++11 Standard! Without the proper protective gear in place, that thing could maim you for life. Instead, I suggest you start with Anthony Williams' book, C++ Concurrency in Action. Chapter 5 has the most comprehensible description of the C++11 memory model I have seen, and in fact it's the only one I know of that points out that C++11 actually offers three different consistency models: sequential consistency, acquire-release consistency, and relaxed consistency. (That alone should give you some idea of what you're in for if you decide to delve into this topic.) I own Williams' book in both electronic and paper form, and I think it's not just the best current treatment of C++11's threading facilities (including, but not limited to, the memory model), it's likely to remain the best for some time to come.
Sources that complement Williams' coverage include the following. Note that anything published before September 2011 may differ from the final C++11 memory model, and the longer the time before September 2011, the higher the likelihood of divergence.
- Shared Memory Consistency Models: A Tutorial, Sarita V. Adve and Kourosh Gharacholloo, digital Western Research Laboratory, September 1995. This paper has nothing to do with C++, much less C++11, but it's the granddaddy of all memory model articles, and if you wish to come to grips with memory model issues, you must understand the material discussed here. Read it twice before proceeding.
- “A Less Formal Explanation of the Proposed C++ Concurrency Memory Model,” Hans-J. Boehm, WG21 Document N2480, 9 December 2007.
- “Foundations of the C++ Concurrency Memory Model,” Hans-J. Boehm and Sarita V. Adve, Proceedings of PLDI’08, June 2008.
- Threads and memory model for C++, Hans Boehm. This is a collection of links to various documents.
- Prism: A Principle-Based Sequential Memory Model for Microsoft Native Code Platforms, Herb Sutter,WG21 Document N2197, 11 March 2007. This does not describe the C++11 memory model. Rather, it describes a similar memory model, and the description in this document is much easier to follow than in most other places.
Thanks for the information and the pointers (no pun intended) of where to look for more information.
ReplyDeleteI write low-level software (much of it in C++, in spite of Linus' advice to the contrary), so this information is very useful.
In the process of writing code, I continually shift from wanting to understand the underlying model, to simply wanting to know that I'm writing solid, safe code that uses the languages features (e.g.,
std::lock_guard, std::atomic like you mentioned.)
I've heard good things about the Williams book, and I've got a long flight coming up and a Kindle Fire with unused capacity, time to go shopping!
After nearly two years of waiting for my preorder, this book landed on my doorstep a month ago, and it has been an absolute pleasure to read so far (I've covered just past half of it).
ReplyDeleteI must admit, I skipped over chapter 5 to jump ahead to a few things I found more immediately interesting (thread pools, task libraries, etc), esp. since I don't plan on writing lock free data structures any time soon.
Still, I am looking forward to finally scratching the itch of figuring out the C++11 memory model :)