Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Simples explanation for now.</p> <p><strong>Lock</strong></p> <p>Is an atomic test for whether this piece of code can proceed</p> <pre><code>lock (myObject) { // Stuff to do when I acquire the lock } </code></pre> <p>This is normally a single CPU instruction that tests and sets a variable as a single atomic operation. More here, <a href="http://en.wikipedia.org/wiki/Test-and-set#Hardware_implementation_of_test-and-set_2" rel="nofollow">http://en.wikipedia.org/wiki/Test-and-set#Hardware_implementation_of_test-and-set_2</a></p> <p><strong>Memory Barrier</strong></p> <p>Is a hint to the processor that it can not execute these instructions out of order. Without it, the instructions could be executed out of order, like in double checked locking, the two null checks could be executed before the lock has.</p> <pre><code>Thread.MemoryBarrier(); public static Singleton Instance() { if (_singletonInstance == null) { lock(myObject) { if (_singletonInstance == null) { _singletonInstance = new Singleton(); } } } } </code></pre> <p>This are also a set of CPU instructions that implement memory barriers to explicitly tell a CPU it can not execute things out of order.</p> <p><strong>Semaphores</strong></p> <p>Are similar to locks except they normally are for more than one thread. I.e. if you can handle 10 concurrent disk reads for example, you'd use a semaphore. Depending on the processor this is either its own instruction, or a test and set instruction with load more work around interrupts (for e.g. on ARM).</p>
 

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