c# - Memory Barrier by lock statement -


I have recently read about memory barriers and revision issues and now I have some confusion about it.

Consider this scenario:

  private object _object1 = null; Private Object _object2 = null; Private bool _using object1 = false; Private object MyObject {get (if (_using object1) {return_object1;} and {return_object2;}} set {if (_usingObject1) {_object1 = value;} and {_object2 = value;}}} private zeros update () {_usingMethod1 = true; SomeProperty = FooMethod (); //. _usingMethod1 = False;}  
  1. update on the < Code> _usingMethod1 = true statement is always executed before receiving or installing the property? Or can we not guarantee because of the restart?

  2. Like us unstable like

      the personal volatile bull You should use _usingMethod1 = false;  
  3. If we use the lock, then we can guarantee that each statement within the lock will be executed in order: < / P>

      Private zero fooMethod () {Object Locker = New Object (); Lock (Locker) {x = 1; Y = A; i ++;}}  

The subject of memory constraints is quite complex. It also visits specialists from time to time when we talk about memory constraints, we are actually connecting two different ideas.

  • Retrieve the fence: A memory hurdle in which the other reads & amp; Writes that before the fence is not allowed to go.
  • Release fence: a memory hurdle in which another reads & amp;
  • >. A memory hazard which both creates is sometimes called full-fence .

    Unstable makes the keyword half-barricades. Readings of volatile fields read cementics. This means that no instructions can be sent after reading or writing any.

    Lock creates a full-fence on both limits (entry and exit). This means that no instructions can be taken before or after each limit.

    However, it is all controversial if we are concerned only with one thread. Order, as it is considered by that thread, is always preserved; Actually, without any basic Granite, a program will never work properly. The real issue is how reads and writes the other threads.

    Then to answer your questions:

    1. From the perspective of the same thread ... yes ... from the perspective of the second thread ... no.

    2. It depends on it can work, but I need to understand better what you are trying to achieve.

    3. From the perspective of the second thread ... does not read and writes that it is free to roam around the boundaries of the lock. They can not go beyond just those boundaries. This is the reason why memory threads for other threads are also important in making.


Comments