Basics of the Memory Model
From the concurrency point of view, there are two main aspects of the memory model:
- What is a memory location?
- What happens if two threads access the same memory location?
Let me answer both questions.
What is a memory location?
A memory location is according to cppreference.com
- an object of scalar type (arithmetic type, pointer type, enumeration type, or
std::nullptr_t), - or the largest contiguous sequence of bit fields of non-zero length.
Here is an example of a memory location:
struct S {
char a; // memory location #1
int b : 5; // memory location #2
int c : 11, // memory location #2 (continued)
: 0,
d : 8; // memory location #3
int e; // memory location #4
double f; // memory location #5
std::string g; // several memory locations
};
First, the object obj consists of seven sub-objects and the two bit fields b, and c share the same memory location.
Here are...