Note that there are some explanatory texts on larger screens.

plurals
  1. POSynchronise 3 threads with 3 condition variables
    text
    copied!<p>I'm writing a program in c++ in a vtk/QT environment. This problem, however, is mostly a question of approach/algorithm.</p> <p>I am stuck in an attempt to synchronise my three running threads: 1. thread: transmit one sample at a time, and adds it to a "output" buffer 2. thread: receives one sample at a time, and adds it to a "input" buffer 3. thread: pulls data from "output" and "input" buffers and ads them to separate plotting buffers, for rendering.</p> <p>I want these threads to be run synchronised, and have there-for tried an approach where I use one condition variable together with a boolian condition for each of the threads, where one thread signals to the next and so fourth in a loop, in the order listed above. However, when I do this I get a deadlock and my program stops. I would really appreciate some input here:)</p> <p>Here's my approach in code:</p> <p>//Initialcondition for the boolian variables:</p> <pre><code>readyForTransmit=true; readyForReceive=false; readyForPlotting=false; </code></pre> <p>Thread 1 - transmit:</p> <pre><code>while(1){ mutex-&gt;Lock(); //waits for condition/signal to proceed while(!readyForTransmit) transmitConditionVariable-&gt;wait(mutex); readyForTransmit=false; mutex-&gt;Unlock(); //Here I transmit my sample transmit(); //Triggers next thread - reception mutex-&gt;Lock(); readyForReceive=true; receiveConditionVariable-&gt;broadcast(); //Have also tried signal(); mutex-&gt;Unlock(); } </code></pre> <p>Thread 2 - receive:</p> <pre><code>while(1){ mutex-&gt;Lock(); //waits for condition/signal to proceed while(!readyForReceive) receiveConditionVariable-&gt;wait(mutex); readyForReceive=false; mutex-&gt;Unlock(); //Here I receive my sample receive(); //Triggers next thread - reception mutex-&gt;Lock(); readyForPlotting=true; plottingConditionVariable-&gt;broadcast(); //Have also tried signal(); mutex-&gt;Unlock(); } </code></pre> <p>Thread 3 - adds to plotting buffers:</p> <pre><code>while(1){ mutex-&gt;Lock(); //waits for condition/signal to proceed while(!readyForPlotting) plottingConditionVariable-&gt;wait(mutex); readyForPlotting=false; mutex-&gt;Unlock(); //Here I adds samples to plotting buffer updatePlottingBuffers(); //Triggers next thread - reception mutex-&gt;Lock(); readyForTransmit=true; transmitConditionVariable-&gt;broadcast(); //Have also tried signal(); mutex-&gt;Unlock(); } </code></pre> <p>In addition to this I push and pull samples thread-safe to and from the buffers. So that shouldn't be a problem.</p> <p>Hope to hear from you! =)</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