Memory Model: preventing store-release and load-acquire reordering

后端 未结 4 1261
情书的邮戳
情书的邮戳 2021-01-01 01:43

It is known that, unlike Java\'s volatiles, .NET\'s ones allow reordering of volatile writes with the following volatile reads from another location. When it is a problem

4条回答
  •  时光取名叫无心
    2021-01-01 02:25

    Let me disagree with the accepted answer from Brian Gideon.

    OmariO your solution to the problem (dummy read) looks perfectly correct to me. As you mentioned correctly, writes to a variable cannot be reordered with a following read from the same variable. If that reordering was possible then it would make the code incorrect in a single-threaded case (the read operation could return not the same value that has been written by the previous write operation). That is it would violate the fundamental rule of any memory model: single-threaded execution of a program must not be logically changed.

    Also guys, Brian and OmariO, please don't mix up memory operations with acquire/release semantics and acquire/release memory fences. E.g. a read-acquire operation is not the same as an acquire fence. They have similar semantics but the distinction between them is very important. The best explanation of that terms that I know is in the great blog of Jeff Preshing:
    Acquire and Release Semantics
    Acquire and Release Fences

提交回复
热议问题