Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is a class producer/consumer problem. Your code as it stands doesn't really make sense - you cannot use a single condition variable for waiting on both sides, (remember you call notify then wait - on the same condition, in the same context.) So anything can happen.</p> <p>You need to refactor your code to use two condition variables, one for the producer, one for the consumer, for example:</p> <p>EDIT: Updated with a pure c++11 implementation which should be correct.</p> <pre><code>#include &lt;atomic&gt; #include &lt;chrono&gt; #include &lt;condition_variable&gt; #include &lt;iostream&gt; #include &lt;mutex&gt; #include &lt;thread&gt; using namespace std; mutex mutex_; condition_variable con_, prd_; atomic_bool done_{false}, process_{false}; // possibly overkill, but hey-ho int counter = 0; // protected resource void consumer() { for (;;) { unique_lock&lt;mutex&gt; lock(mutex_); // acquire the mutex before waiting // this is the correct way to wait on the condition (as highlighted by @Dale) con_.wait(lock, []{ return process_.load() || done_.load(); }); if (!done_.load()) { cout &lt;&lt; counter &lt;&lt; endl; process_.store(false); // we're done with this item lock.unlock(); // release before notifying prd_.notify_one(); } else break; // we're done completely } } void producer() { unique_lock&lt;mutex&gt; lock(mutex_); for (int i = 0; i &lt; 10; ++i) { this_thread::sleep_for(chrono::milliseconds(500)); ++counter; process_.store(true); // indicate that there is data con_.notify_one(); // wait for the consumer to complete prd_.wait(lock, []{ return !process_.load(); }); } done_.store(true); // we're done lock.unlock(); // release before notifying con_.notify_one(); } int main(void) { thread c(consumer); producer(); c.join(); } </code></pre> <p>So now what happens is, the consumer waits on it's condition (and only the producer will call <code>notify()</code> on this), and the producer, once it has produced something calls this notify which will wake the client up. The client will then call <code>notify()</code> to wake the producer up.</p> <p>The above updated approach should not suffer the spurious wake up problem as I highlighted before. This negates the need for a <code>timed_wait</code>.</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.
 

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