C++11 and C++14: The Foundation
Multithreading was introduced in C++11. This support consists of two parts: A well-defined memory model, and a standardised threading interface. C++14 added reader-writer locks to the multithreading facilities of C++.
Memory Model
The foundation of multithreading is a well-defined memory model. This memory model has to deal with the following aspects:
- Atomic operations: operations that can be performed without interruption.
- Partial ordering of operations: a sequence of operations that must not be reordered.
- Visible effects of operations: guarantees when operations on shared variables are visible in other threads.
The C++ memory model was inspired by its predecessor: the Java memory model. Unlike the Java memory model, however, C++ allows us to break the constraints of sequential consistency, which is the default behaviour of atomic operations.
Sequential consistency provides two guarantees.
- The instructions of a program are executed...