In 2004, Andrei and I coauthored an article about why it was not possible to write a safe implementation of double-checked locking in standard C++. Between compiler optimizers performing code motion and hardware MMUs reordering loads and stores, there was no way to prevent transformations that could cause safe-looking source code to yield incorrect runtime behavior.
But that was for C++98, which, because it had no concept of threads, had no memory model, so it offered no guarantees regarding the visibility of reads and writes across threads. C++0x does have a memory model, and the changes to the language and library are significant. Sequence points no longer exist, having been replaced with the “sequenced before” and “happens before” relationships. Atomic types offer memory visibility guarantees not offered by non-atomic types, and compilers must generate code that respects these guarantees. These guarantees make it possible to prevent the kind of unwanted code motion and memory access reorderings that bedevil double-checked locking in C++98, although they’re not enough: the core language must also constrain the relationship between memory allocation and object construction in a new expression. C++0x provides such constraints.
Like most languages that recognize the existence of multiple threads, C++0x offers programmers a mechanism for ensuring sequential consistency in concurrent code. In contrast to other languages (and in recognition of C++’s role as a systems programming language), C++0x offers fine-grained control over the constraints imposed on compilers and hardware when optimizing, making it possible to implement consistency models weaker — and hence potentially faster — than sequential consistency.
This talk will address the topics mentioned above: double-checked locking (briefly — I’ll assume you’ve read Andrei’s and my above-mentioned article in advance), compiler optimizations, memory access reorderings, “sequenced before” and “happens before” relations, atomic types, memory consistency models, and how they relate to both correctness and performance. Also, because it’s pretty much obligatory in this kind of talk, I’ll address the potentially confusing relationship between atomic and volatile
types.
I welcome your feedback on this talk topic.
Scott
April 12, 2011 at 5:29 pm
Scott,
this will be a very, very useful topic. I wish I lived closer, so that I could attend your talks.
Regards,
Peter.
April 14, 2011 at 11:35 am
[…] will give a great new talk on “The C++0x Memory Model and Why You Care” that will cover topics of interest to anybody who cares about concurrency and parallel […]
April 28, 2011 at 9:51 am
[…] efficiently processing big data sets, taking advantage of the computational capabilities of GPUs, understanding the C++0x memory model, and more (all created just for C&B), plus the chance to exchange experiences and ideas with […]
June 14, 2011 at 10:04 pm
[…] session, and there are two reasons for that. First, I have a feeling that my treatment of the C++0x memory model may run longer than a single session, and if that’s the case, I’ll give just that talk […]
July 31, 2011 at 10:40 am
[…] My talk on the C++0x Memory Model doesn’t really assume much background beyond a familiarity with the existence of multithreaded code, but if you’d like to familiarize yourself with some of the key motivating issues I’ll be addressing, I recommend the article on Double-Checked Locking that Andrei and I wrote in 2004. That article will also introduce you to the double-checked locking pattern, which I’ll briefly discuss in the talk. […]
November 12, 2011 at 4:34 am
volagratis…
[…]Session Announcement: The C++0x Memory Model and Why You Care « C++ and Beyond[…]…
December 4, 2011 at 4:10 pm
รายได้เสริมทางเน็ต…
[…]Session Announcement: The C++0x Memory Model and Why You Care « C++ and Beyond[…]…