Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my take on the subject and to attempt to provide a quasi-complete list in one answer. If I run across any others I will edit my answer from time to time.</p> <p>Mechanisms that are generally agreed upon to cause implicit barriers:</p> <ul> <li>All <code>Monitor</code> class methods including the C# keyword <code>lock</code></li> <li>All <code>Interlocked</code> class methods.</li> <li>All <code>Volatile</code> class methods (.NET 4.5+).</li> <li>Most <code>SpinLock</code> methods including <code>Enter</code> and <code>Exit</code>.</li> <li><code>Thread.Join</code></li> <li><code>Thread.VolatileRead</code> and <code>Thread.VolatileWrite</code></li> <li><code>Thread.MemoryBarrier</code></li> <li>The <code>volatile</code> keyword.</li> <li>Anything that starts a thread or causes a delegate to execute on another thread including <code>QueueUserWorkItem</code>, <code>Task.Factory.StartNew</code>, <code>Thread.Start</code>, compiler supplied <code>BeginInvoke</code> methods, etc.</li> <li>Using a signaling mechanism such as <code>ManualResetEvent</code>, <code>AutoResetEvent</code>, <code>CountdownEvent</code>, <code>Semaphore</code>, <code>Barrier</code>, etc.</li> <li>Using marshaling operations such as <code>Control.Invoke</code>, <code>Dispatcher.Invoke</code>, <code>SynchronizationContext.Post</code>, etc.</li> </ul> <p>Mechanisms that are speculated (but not known for certain) to cause implicit barriers:</p> <ul> <li><code>Thread.Sleep</code> (proposed by myself and possibly others due to the fact that code which exhibits a memory barrier problem can be fixed with this method)</li> <li><code>Thread.Yield</code></li> <li><code>Thread.SpinWait</code></li> <li><code>Lazy&lt;T&gt;</code> depending on which <code>LazyThreadSafetyMode</code> is specified</li> </ul> <p>Other notable mentions:</p> <ul> <li>Default add and remove handlers for events in C# since they use <code>lock</code> or <code>Interlocked.CompareExchange</code>.</li> <li>x86 stores have release fence semantics</li> <li>Microsoft's implemenation of the CLI has release fence semantics on writes despite the fact that the ECMA specification does not mandate it.</li> <li><code>MarshalByRefObject</code> seems to suppress certain optimizations in subclasses which may make it appear as if an implicit memory barrier were present. Thanks to <a href="https://stackoverflow.com/a/10308000/158779">Hans Passant</a> for discovering this and bringing it to my attention.<sup>1</sup></li> </ul> <hr> <p><sup>1</sup><em>This explains why <code>BackgroundWorker</code> works correctly without having <code>volatile</code> on the underlying field for the <code>CancellationPending</code> property.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload