Threads and Shared Variables in C++11

Share:

Listens: 0

GoingNative 2012 Sessions (HD)

Miscellaneous


The C++11 standard introduces threads into the language, and carefully defines the meaning of variables shared between threads. The design is based on the idea that meaningful multithreaded programs should, by default, behave as though threads were executed in simple interleaved fashion. This allows efficient execution only because "data races" (simultaneous non-read-only accesses to shared variables) are disallowed.  Although this basic approach is shared with some other languages, and with threads interfaces like pthreads, we tried hard to avoid their mistakes.  For example, C++11 provides a comprehensive atomic operations library, which makes it much more feasible to avoid data races in real low-level code.I'll give a very quick overview of thread support in C++11, and then focus on the behavior of shared variables, often called the "memory model". I'll try to explain why, after more than 40 years of programming with shared variables, shared variables and atomic operations were still controversial, and why the C++11 approach is the right one.