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

  • Home
  • SEARCH
  • 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 8459399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:19:13+00:00 2026-06-10T13:19:13+00:00

I have implemented a sample program to understand how wait_for and wait_until works in

  • 0

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.

  • 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-10T13:19:15+00:00Added an answer on June 10, 2026 at 1:19 pm

    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.

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

Sidebar

Related Questions

I have Implemented a simple program which simulates $ls -l | wc -c command
I have implemented an android program that can receive simple messages using the MJSIP,
I have implemented a program, to print the document to the specific printer using
I have implemented a simple entity ejb with a @version annotation. I expect that
I have implemented a simple linux shell in c. Now, I am adding some
I have implemented a simple RSS reader in Java. I hit a URL and
I have implemented my own simple version of a navigation window, mainly because navigation
I've got a simple C class I have implemented, using function pointers in a
I am creating a simple messaging system in WCF and have implemented a minimal
Very simple Qt GUI application: On the scene I have multiple circles implemented as

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.