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 8057279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:59:01+00:00 2026-06-05T08:59:01+00:00

A condition variable can be used to signal to other threads that something has

  • 0

A condition variable can be used to signal to other threads that something has happened:

mutex m;
condition_variable cv;

thread t1([&cv]{
    // processing
    ...
    cv.notify_one();
});
...
unique_lock<std::mutex> lck(m);
cv.wait(lck);

But as you can see, there is a window of opportunity that the thread processing is finished and the notification is passing by before we get to wait to be notified, so we will wait forever.

In that case, a common solution is the use of a flag:

mutex m;
condition_variable cv;
bool done = false;

thread t1([&cv,&done]{
    // processing
    ...
    done = true; 
    cv.notify_one();
});
...
unique_lock<std::mutex> lck(m);
cv.wait(lck, [&done]{return done;});

Is using a flag the common way to use a condition_variable, or is my interpretation wrong?

  • 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-05T08:59:02+00:00Added an answer on June 5, 2026 at 8:59 am

    A condition variable should always be associated with some condition, which you should test:

        unique_lock<mutex> lck(m);
        while (!something) 
          cv.wait(lck);

    The condition is checked while the mutex is held, so that implies the mutex should protect the data associated with the condition, so you know it won’t change between testing it and waiting.

    The test is a while not just an if because some condition variable implementations (including pthreads-based ones) may wake up spuriously i.e. when noone signalled it, so you should check the condition in a loop and re-wait while it isn’t true. There’s an overload of wait that takes a predicate and automatically handles spurious wake ups by waiting until the predicate returns true, e.g. here’s the above example modified to use a lambda that checks the condition:

        unique_lock<mutex> lck(m);
        cv.wait(lck, [&] { return something; });

    (In simple cases I find the explicit while loop easier to read.)

    A condition variable that is in use can be thought of as a 3-tuple consisting of the condition variable, a mutex and a predicate, which are conceptually bound together by being used together to wait on the condition variable. All concurrent waits on a particular condition variable object must use the same mutex, and will generally also be waiting on the same predicate (or a related predicate that depends on the same data, protected by the same mutex.)

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

Sidebar

Related Questions

With Ruby, how can I set a variable based on a condition? Something like:
Are condition variables & monitors used in C#? Can someone give me an example?
pthread_cond_broadcast is used to wake up several threads waiting on a condition variable. But,
Suppose a condition variable is used in a situation where the signaling thread modifies
Normally, we can signal only one variable like pthread_cond_signal (condition) However, I want wake
I've written some pthread code that use timed waits on a condition variable but
How do I make a: if str(variable) == [contains text]: condition? (or something, because
This method is updating test variable. But the problem is that if condition does
I've set a variable so it can be used from the controller to pass
Can't I use double condition in where clause in query. LIke I have used

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.