Note that there are some explanatory texts on larger screens.

plurals
  1. POThread suspends itself and another one resumes it afterwards - sometimes second event happens first
    text
    copied!<p>I have two threads; thread 2 provides necessary data for thread 1 and can start working on another piece of data in parallel with thread 1 processing it, but won't move onto the next piece if thread 1 is slower (yes, I'm aware about "Thread 1 reads what Thread 2 writes at the same time", I have a protection in form of two buffers instead of one). The code is below:</p> <pre><code>Thread 1: while(!shouldexit) { // some code Thread1_Ready = true; SuspendThread (Thread1_Handle); } Thread 2: while(!shouldexit) { while(!Thread1_Ready) { // do nothing } // some other code ResumeThread(Thread1_Handle); } </code></pre> <p>The problem is, sometimes the whole code inside Thread 2 happens faster than <code>SuspendThread</code> happens in Thread 1. The result is that Thread 1 remains suspended until yet another cycle of Thread 2 passes. This leads to one piece of data not processed by Thread 1 (not very good, but acceptable in my application), and, which is more serious, when the Stop button triggers <code>shouldexit = true</code>, Thread 1 just stays in suspended mode indefinitely.</p> <p>Two possible solutions that I thought of wouldn't work either: 1 - put <code>Thread1_Ready = true;</code> after <code>SuspendThread(Thread1_Handle);</code> (the code <code>Thread1_Ready = true;</code> never executes, obviously) 2 - put the line <code>SuspendThread(Thread1_Handle);</code> from Thread 1 to the beginning of Thread 2, right before <code>// some other code</code>, but this will cause a delay during which Thread 1 can re-enter the main loop and it will be very unpredictable.</p> <p>Can this situation be solved differently?</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