Why volatile and MemoryBarrier do not prevent operations reordering?

后端 未结 4 1255
耶瑟儿~
耶瑟儿~ 2021-02-05 17:10

If I understand meaning of volatile and MemoryBarrier correctly than the program below has never to be able to show any result.

It catches reordering of write operations

4条回答
  •  灰色年华
    2021-02-05 17:40

    1. If you use MemoryBarrier in writer, why don't you do that in checker? Put Thread.MemoryBarrier(); before int tempA = a;.

    2. Calling Thread.MemoryBarrier(); so many times blocks all of the advantages of the method. Call it only once before or after a = 1;.

提交回复
热议问题