Interlocked and Memory Barriers

前端 未结 7 2033
情书的邮戳
情书的邮戳 2021-02-02 18:04

I have a question about the following code sample (m_value isn\'t volatile, and every thread runs on a separate processor)

void Foo() // executed by thr         


        
7条回答
  •  佛祖请我去吃肉
    2021-02-02 18:33

    The interlocked exchange operations guarantee a memory barrier.

    The following synchronization functions use the appropriate barriers to ensure memory ordering:

    • Functions that enter or leave critical sections

    • Functions that signal synchronization objects

    • Wait functions

    • Interlocked functions

    (Source : link)

    But you are out of luck with register variables. If m_value is in a register in Bar, you won't see the change to m_value. Due to this, you should declare shared variables 'volatile'.

提交回复
热议问题