MaxMarcucci
MaxMarcucci

Reputation: 175

condition_variable wait in C++

I have been trying condition_variable::wait and precisely I am using :

 template< class Predicate >
 void wait( std::unique_lock<std::mutex>& lock, Predicate pred );

but I am a little bit confused when I executed it. This the simple example:

 std::condition_variable cv;
 std::mutex cv_m;
 bool flag = false;

 void waits()
 {
   std::unique_lock<std::mutex> lk(cv_m);
   std::cerr << "Waiting... \n";

   cv.wait(lk,[]() {return flag == true;});
   std::cerr << "...finished waiting " << flag << std::endl;
 }

 void signals()
 {
    //std::this_thread::sleep_for(std::chrono::seconds(1));

   std::unique_lock<std::mutex> lk(cv_m);
   std::cout <<"Counting...."<< std::endl;
   flag = true;
     for(int i = 0; i < 5; i++) {
       std::cout << i << std::endl;
   }

  cv.notify_all();

 }

 int main()
 {
   std::thread t1(waits), t2(waits), t3(waits), t4(signals);
   t1.join();
   t2.join();
   t3.join();
   t4.join();

   return 0;
 }

if I am delay the signals() with

  std::this_thread::sleep_for(std::chrono::seconds(1));

it works as expected while if I put the above line under comment the output it is a mess. Shouldnd the wait condition put on hold the execution of waits() anyway ? Why do I need a delay the output changes every time and most of the time messed up ?

An example of the output

 Waiting... 
 Counting....
 0
 1
 2
 3
 4
 Waiting... 
 ...finished waiting 1
 Waiting... 
 ...finished waiting 1
 ...finished waiting 1

Thank you for any help

Upvotes: 0

Views: 2496

Answers (1)

dohashi
dohashi

Reputation: 1841

You have a race condition. It is possible for t4 to grab the mutex and run to completion before the other threads have a chance to get into the wait. Thus they miss the signal.

You need to synchronize so that the t1, t2, and t3 get into the wait before t4 starts. Anytime a sleep seems to fix a problem in threaded code, you probably have a race condition that requires real synchronization to fix properly.

Upvotes: 1

Related Questions