Does Monitor.Wait ensure that fields are re-read?

后端 未结 3 1778
小蘑菇
小蘑菇 2021-02-02 11:08

It is generally accepted (I believe!) that a lock will force any values from fields to be reloaded (essentially acting as a memory-barrier or fence - my terminology

3条回答
  •  爱一瞬间的悲伤
    2021-02-02 11:40

    Maybe I can help you this time... instead of using a volatile you can use Interlocked.Exchange with an integer.

    if (closing==1) {       // <==== (2) access field here
        value = default(T);
        return false;
    }
    
    // somewhere else in your code:
    Interlocked.Exchange(ref closing, 1);
    

    Interlocked.Exchange is a synchronization mechanism, volatile isn't... I hope that's worth something (but you probably already thought about this).

提交回复
热议问题