Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have no <a href="http://www.codeproject.com/KB/threads/thread_synchronization.aspx" rel="nofollow">synchronization</a> protecting the 'done' variable. When an object or variable may be accessed in one thread while another thread is, or might be, modifying it, some form of synchronization is needed.</p> <p>The ways it can actually fail are many. For example, consider this loop:</p> <pre><code>private void test() { do { if (done) { Invoke(new setLabelMethod(setLabelValue), "yeah"); done = false; } Thread.Sleep(500); } while (running); } </code></pre> <p>Suppose the compiler knows that <code>Thread.Sleep</code> does not modify <code>done</code> or <code>running</code>. It may conclude that nothing in this loop modifies <code>done</code> or <code>running</code> (assuming <code>done</code> is false when the loop is entered) and therefore it can cache <code>done</code> (and <code>running</code>!) in registers across calls to <code>Thread.Sleep</code>.</p> <p>In other words, it can 'optimize' your loop to:</p> <pre><code>private void test() { if (done) Invoke(new setLabelMethod(setLabelValue), "yeah"); done = false; if (running) while(1) Thread.Sleep(500); } </code></pre> <p>Notice that this is a bona fide optimization, if <code>done</code> and <code>running</code> don't have their values changed by other threads. The compiler is free to assume this since it violates the rules to modify a value in one thread while another thread is or might be accessing it without synchronization, this thread might be accessing those variables, and this code contains no synchronization functions.</p> <p>Of course, your code doesn't have to worry about this if you follow the rules. The compiler is prohibited from making 'optimizations' that break your code and you don't have to worry about how it does it. But that only applies if you follow the rule. (In practice, the compiler will not cache a value in a register across a call to a synchronization function and volatile variables will never be cached in registers.)</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. 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.
 

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