Search code examples
c++c++11condition-variable

C++ 11 - condition_variable - wait_until not working as expected


I have implemented a sample program to understand how wait_for and wait_until works in C++11.

Code -

#include <iostream>
#include <future>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <thread>

using namespace std;
using namespace std::chrono;

condition_variable cv;
mutex m;
bool flagValue=false;

int sampleFunction(int a)
{
    cout<<"Executing sampleFunction...";
    cout.flush();
    this_thread::sleep_for(seconds(5));
    cout<<"Done."<<endl;
    return a;
}

void sampleFunctionCond(int a)
{
    lock_guard<mutex> lg(m);
    cout<<"Executing sampleFunctionCond...";
    cout.flush();
    this_thread::sleep_for(seconds(5));
    cout<<"Done."<<endl;
    cout<<"Value : "<<a<<endl;
    flagValue=true;
    cv.notify_one();
    return;
}

int main()
{
   unique_lock<mutex> ul(m);
   future<int> f1=async(launch::async,sampleFunction,10);
   future_status statusF1=f1.wait_for(seconds(1));
   if(statusF1==future_status::ready)
    cout<<"Future is ready"<<endl;
   else if (statusF1==future_status::timeout)
    cout<<"Timeout occurred"<<endl;
   else if (statusF1==future_status::deferred)
    cout<<"Task is deferred"<<endl;
   cout<<"Value : "<<f1.get()<<endl;

   cv_status statusF2;
   thread t1(sampleFunctionCond,20);
   t1.detach();
   while(!flagValue)
   {
       statusF2=cv.wait_until(ul,system_clock::now()+seconds(2));
       if(statusF2==cv_status::timeout)
       {
       cout<<"Timeout occurred."<<endl;
       break;
       }
       else
       {
       cout<<"Condition variable is ready or spurious wake up occurred."<<endl;
       }
   }
}

Output -

Executing sampleFunction...Timeout occurred
Done.
Value : 10
Executing sampleFunctionCond...Done.
Value : 20
Timeout occurred.

sampleFunction is working as expected as its printing "Timeout occurred" before "Done" but same is not the case with sampleFunctionCond. Though it knows wait_until has timed out but its printing the message after the function sampleFunctionCOnd has finished executing.

Can someone please help me understand this? Thanks.


Solution

  • There is a race condition in statusF2=cv.wait_until(...); statement. What may happen is that the wait has timed out and is about to return. To return it needs to reacquire the mutex. At the same time the other thread has acquired the mutex. So, statusF2=cv.wait_until(...); can't return till the other thread has set flagValue to true and released the mutex.

    To fix the code the value of flagValue must be checked before checking whether the wait timed out.