Take the 2-minute tour ×

This is a question about the C++ standard. I only have access to the draft standard so if this is different in the official one, I apologise. Also if I've misunderstood how this works please feel free to correct me.

Assume I have two threads, one writing to a string and one making a copy of the contents of that string. I protect access to them using a std::mutex myMutex; I know you should in general use the RAII classes for locks, I just used lock and unlock explicitly to make the example more explicit.

// Global variable
std::string message;
std::mutex myMutex;

// Thread one
message = "Hello";

// Thread two
std::string copy = message;

My understanding is that in order for this to work reliably between threads, thread one must perform a Release operation after setting the string, and thead two must perform an Acquire before reading the string.

Reading the draft standard for C++11 I can't see anything that states that std::mutex does this, although it's fairly obvious that it's expected to or the mutex would be useless for anything.

Can someone point me to the relevent section to look at? The wording in the standard is often not exactly clear to a casual reader :)

share|improve this question
1 ff. not strong enough? –  PlasmaHH Nov 13 '12 at 11:05
That appears to only say that the class shall provide lock and unlock functions which will behave as atomic operations. Without giving any more specific information –  jcoder Nov 13 '12 at 11:10
Are you basically asking what the difference between "lock" and "acquire", as well as between "unlock" and "release" is? My understanding is that locking (the Standard also uses the word own) implies acquiring the mutex, and unlocking it implies releasing the mutex. –  jogojapan Nov 13 '12 at 11:12
I was looking for somewhere in the standard that required that to be true. The link you gave and the comments others linked to do that, thanks :) –  jcoder Nov 13 '12 at 11:21
add comment

1 Answer

up vote 8 down vote accepted


Synchronization: Prior unlock() operations on the same object shall synchronize with (1.10) [m.lock()].

Under 1.10p5,

[...] For example, a call that acquires a mutex will perform an acquire operation on the locations comprising the mutex. Correspondingly, a call that releases the same mutex will perform a release operation on those same locations. Informally, performing a release operation on A forces prior side effects on other memory locations to become visible to other threads that later perform a consume or an acquire operation on A. [...]

share|improve this answer
Thank you, I missed the part in 1.10, that makes the other paragraphs much more useful. –  jcoder Nov 13 '12 at 12:59
add comment

Your Answer


