Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think there is likely to be a slight conceptual problem here.</p> <p>Firstly can I suggest that the </p> <pre><code>while(!isDone) </code></pre> <p>is not a great way of waiting - It's known as 'spinning' and allows the thread to use processor time when it's not doing anything, which is not efficient. (Spinning on a lock can be ok in some specific circumstances, but in a user app is generally not a good plan.)</p> <p>The lock, making one thread wait while the other processes is much better.</p> <p>Now, as to your specific problem. It's possible that the isDone flag read in the while test has been optimised out (i.e. the compiler 'knows' that it isn't going to change, so it doesn't put in any code to get it from memory again - it just tests the same CPU register.) You can overcome this by using the 'volatile' modifier to tell the compiler it must re-get the value from memory. It's also possible that the main thread, spinning, is starving the other thread so it never gets a chance to set the flag (although one would hope it would eventually, in a 'fair' system.)</p> <p>Regardless of the latter, having a thread spin while waiting for another thread to finish proccessing is to be avoided, unless having checked the flag, it goes off to do something useful (such as updating the GUI) instead.</p> <p>You need to be very careful with multi-threaded designs - they're intricate and can have obscure and hard-to-predict behaviour (but are well worth doing for the right circumstances.)</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