给定两个线程之间的通用消息传递模式:
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)
问:如果'M1'是一个通用互斥体,当使用C++或C实现软件时,Thread-B是否总是保证有正确的'Msg'内容?问:这种模式对于操作系统和/或处理器配置的某些组合是否不能正常工作?(我主要关注 Linux、Windows、Mac OS X、VxWorks、Green Hills 和 Micrium 等操作系统)
我的理解(可能不正确)是,通过锁定和/或解锁互斥锁“M1”实现的关键部分将导致执行内存屏障/栅栏指令,这将确保处理器/核心缓存的一致性;因此 Thread-B 保证读取 'Msg' 的正确内容。但是,我很难找到表明上述“一般模式”是正确的权威文档。