Given a generic message passing pattern between two threads:
Thread-A Thread-B
------------------- ---------------------
LockMutex(M1)
Get memory for Msg
from shared resource
UnlockMutex(M1)
set Msg attributes (content)
(note: is *NOT* performed
within a critical section--
once the Msg's contents
have been set, no other
writes to the Msg
is performed)
LockMutex(M1)
place pointer of Msg
within Thread-B's
message queue.
UnlockMutex(M1)
notify Thread-B
of new Msg
------------------- ---------------------
LockMutex(M1)
extract pointer to Msg from
queue.
UnlockMutex(M1)
read contents of Msg
(note: Thread-B only *READS*
the contents of Msg)
Q: If 'M1' is a generic mutex, is Thread-B ALWAYS guaranteed to have the correct contents of 'Msg' when C++ or C is used to implement the software? Q: Will this pattern not work correctly for certain combinations of operating systems and/or processor configurations? (Am mostly concerned with OSes such as Linux, Windows, Mac OS X, VxWorks, Green Hills, and Micrium)
My understanding (which could be incorrect) is that the critical section implemented by the locking and/or unlocking of mutex 'M1' will cause a memory barrier/fence instruction to be executed which will ensure processor/core cache coherence; thus Thread-B is guaranteed to read the correct contents of 'Msg'. However, I'm having some difficulty locating authoritative documentation which indicates the above "general pattern" is correct.