Demystifying race conditions and data races
In C++, multithreading support was first introduced with the C++11 version of the language. One of the key elements provided by the C++11 standard to facilitate multithreading is the memory model. The memory model tackles two problems: the layout of objects in memory and the concurrent access to these objects. In C++, all data is represented by objects, which are blocks of memory that have various properties such as type, size, alignment, lifetime, value, and an optional name. Each object remains in memory for a specific period of time and is stored in one or more memory locations, depending on whether it is a simple scalar object or a more complex type.
In the context of multithreaded programming in C++, it is crucial to consider how to tackle concurrent access by multiple threads to shared objects. If two or more threads try to access different memory locations, there is usually no problem. However, when threads attempt to write in the...