I have a non-atomic variable my_var
and an std::mutex mut
. I assume up to this point in the code, the programmer has followed this rule:
Assuming this, Thread1
performs the following:
my_mut.lock();
my_var.modify();
my_mut.unlock();
Here is the sequence of events I imagine in my mind:
- to
my_mut.lock();
, there were possibly multiple copies ofmy_var
in main memory and some local caches. These values do not necessarily agree, even if the programmer followed the rule. - By the instruction
my_mut.lock();
, all writes from the previously executedmy_mut
critical section are visible in memory to this thread. my_var.modify();
executes.-
my_mut.unlock();
, there are possibly multiple copies ofmy_var
in main memory and some local caches. These values do not necessarily agree, even if the programmer followed the rule. The value ofmy_var
at the end of this thread will be visible to the next thread that locksmy_mut
, by the time it locksmy_mut
.
I have been having trouble finding a source that verifies that this is exactly how std::mutex
should work. I consulted the C++ standard. From ISO 2013, I found this section:
Is my understanding of std::mutex
correct?
