Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will address your question as one about multithreading in a high-level language, rather than discussing CPU pipeline optimization.</p> <blockquote> <p>Can anyone explain how to correctly deal with the possibility of reordered operations in a multi-threaded environment?</p> </blockquote> <p>Most, if not all, <strong>modern high-level multithreaded languages provide constructs for managing this</strong> potential for the compiler to reorder the logical execution of instructions. In C#, these include field-level constructs (<code>volatile</code> modifier), block-level constructs (<code>lock</code> keyword), and imperative constructs (<code>Thead.MemoryBarrier</code>).</p> <p>Applying <strong><code>volatile</code></strong> to a field causes all access to that field in the CPU/memory to be executed in the same relative order in which it occurs in the instruction sequence (source code).</p> <p>Using <strong><code>lock</code></strong> around a block of code causes the enclosed instruction sequence to be executed in the same relative order in which it occurs in the parent block of code.</p> <p>The <strong><code>Thread.MemoryBarrier</code></strong> method indicates to the compiler that the CPU must not reorder memory access around this point in the instruction sequence. This enables a more advanced technique for specialized requirements.</p> <p>The techniques above are described in order of increasing complexity and performance. As with all concurrency programming, determining when and where to apply these techniques is the challenge. When synchronizing access to a single field, the <code>volatile</code> keyword will work, but it could prove to be overkill. Sometimes you only need to synchronize writes (in which case a <code>ReaderWriterLockSlim</code> would accomplish the same thing with much better performance). Sometimes you need to manipulate the field multiple times in quick succession, or you must check a field and conditionally manipulate it. In these cases, the <code>lock</code> keyword is a better idea. Sometimes you have multiple threads manipulating shared state in a very loosely-synchronized model to improve performance (not typically recommended). In that case, carefully placed memory barriers can prevent stale and inconsistent data from being used in threads.</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.
    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