C# enforcing order of statement execution

后端 未结 4 830
夕颜
夕颜 2021-02-13 15:15

My question is about order of execution guarantees in C# (and presumably .Net in general). I give Java examples I know something about to compare with.

For Java (from \"

4条回答
  •  时光说笑
    2021-02-13 15:36

    I'm worried that you're even asking this but since you asked.

    y = 10;
    Thread.MemoryBarrier();
    x = 5;
    Thread.MemoryBarrier();
    a = b + 10;
    Thread.MemoryBarrier();
    // ...
    

    From msdn

    Synchronizes memory access as follows: The processor executing the current thread cannot reorder instructions in such a way that memory accesses prior to the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier.

提交回复
热议问题