Information on the C++11 Memory Model via scottmeyers

Tuesday, April 24, 2012

Information on the C++11 Memory Model

I've been receiving a number of inquiries about where to turn for information about the C++11 memory model (technically the memory consistency model). Having prepared and given a talk on the topic at last year's C++ and Beyond, I can tell you that the C++11 memory model is one of the most technically challenging topics I've ever tried to master. The good news is that if your goal is simply to write well-behaved multithreaded programs, you don't need to know any of the details of the memory model. All you need to remember is to avoid data races in your programs, and the simplest way to do that is to follow the advice that multithreaded developers have been following for years: access shared mutable data only when you know you have exclusive access to it, typically by acquiring a mutex before the access and releasing the mutex after the access. C++11 offers RAII classes to automate the release part of this protocol (std::lock_guard and std::unique_lock), so this is easier than ever to do. In C++11, you can also use atomic data types (e.g., std::atomic) to ensure exclusive access with often greater efficiency, but conceptually these are just data types where, on each access, C++ itself handles the moral equivalent of locking and unlocking the appropriate mutex. (In practice, special machine instructions that avoid the need for mutexes are typically employed.) So, as I said, if your interest in the C++11 memory model is to know how to write threaded programs that will run correctly, you can essentially ignore the model itself.

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.

Good luck!

Scott

posted @ 2015-11-12 18:56  scott_h  阅读(212)  评论(0编辑  收藏  举报