Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:46:18+00:00 2026-06-18T12:46:18+00:00

i am trying following code to test boost condition variable for synchoronization, this code

  • 0

i am trying following code to test boost condition variable for synchoronization,
this code does sync. but it shows only 4 values what is wrong here ? , how i can fix it ?

using vs 2012 on windows 7

thanks in advance.

#include <iostream>
#include <queue>

#include "boost\thread.hpp"
#include "boost\timer.hpp"

using namespace std;

int counter;

boost::mutex m;
boost::condition_variable CworkDone;
bool workdone = true;

bool procOn = true;

void display()
{
while (procOn == true)
{
    boost::mutex::scoped_lock lock(m);      

    if (workdone)
    {
        cout<<counter<<endl;
        CworkDone.notify_one();
        workdone = false;
    }   
    else 
    {
        CworkDone.wait(lock);
    }
}

}

void increment()
{
for(int i = 0 ; i <10 ; ++i)
{

    boost::mutex::scoped_lock lock(m);

    if (!workdone)
    {
        boost::this_thread::sleep(boost::posix_time::millisec(500));
        ++counter;
        workdone = true;
        CworkDone.notify_one();
    }
    else
    {
        CworkDone.wait(lock);
    }
}
procOn = false;
}

   int main()
{
boost::thread dispthread(display);
boost::thread incthread(increment);
dispthread.join();
incthread.join();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:46:19+00:00Added an answer on June 18, 2026 at 12:46 pm

    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.

    You need to refactor your code to use two condition variables, one for the producer, one for the consumer, for example:

    EDIT: Updated with a pure c++11 implementation which should be correct.

    #include <atomic>
    #include <chrono>
    #include <condition_variable>
    #include <iostream>
    #include <mutex>
    #include <thread>
    
    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<mutex> 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 << counter << 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<mutex> lock(mutex_);
      for (int i = 0; i < 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();
    }
    

    So now what happens is, the consumer waits on it’s condition (and only the producer will call notify() on this), and the producer, once it has produced something calls this notify which will wake the client up. The client will then call notify() to wake the producer up.

    The above updated approach should not suffer the spurious wake up problem as I highlighted before. This negates the need for a timed_wait.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do the following: //Code under test function Foo() { this.do_something_interesting =
I am trying to test the following JavaScript code, which is meant to keep
I am trying to used following code but I am not getting good performance
i am trying to make sense of the following result. The test case code
Hello I am trying to create a stacked barplot using the following code: test
I have the following code which i m trying to compile: #include <boost/filesystem/convenience.hpp> #include
I am trying to understand how Boost memory mapped files work. The following code
I am trying the following code, with R 2.13: aa <- list() aa[test] <-
I executed the following C# code to Test environment and PROD server: This code
I'm trying out the following code: public partial class Test: Window { public Test(ref

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.