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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:38:03+00:00 2026-06-08T16:38:03+00:00

I have thread 1 executing the following code: unique_lock<mutex> ul(m); while(condition == true) cv.wait(ul);

  • 0

I have thread 1 executing the following code:

unique_lock<mutex> ul(m);
while(condition == true)
    cv.wait(ul);

And thread 2 executing this code:

condition = false;
cv.notify_one();

Unfortunately I’m hitting a timing issue:

T1: condition checks true
                            T2: condition set to false
                            T2: cv.notify_one()
T1: cv.wait()

Thread 1 misses the notification completely and remains blocked on wait(). I tried using the version of wait() which takes a predicate but with essentially the same result. That is, the body of the predicate performs the check, but before it returns, the condition’s value is changed and the notification is sent. The predicate then returns.

How can I fix this?

  • 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-08T16:38:05+00:00Added an answer on June 8, 2026 at 4:38 pm

    You should fix this race condition by having thread 2 lock the condition’s mutex before changing the flag.

    You are describing a typical race condition that happens for unprotected flags and conditions. These race conditions are the reason for the mutex lock pattern in condition usage. Put simply, always have a mutex protect the variables involved in checking a condition value.

    In code for thread 2:

    unique_lock<mutex> ul(m);
    condition = false;
    cv.notify_one();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting following error while executing my java code. What is this error
I have a thread that does the following: 1) Do some work 2) Wait
I have this following java code skeleton - try { Question q = null;
I have a strange bit of functionality going on when executing the following code
I have the following code: (ns test-hook.core) (defn -main [] (.addShutdownHook (Runtime/getRuntime) (Thread. #(println
I have thread where downloading xml file a this xml file i want to
Is if almost always required to have thread syncing (i.e. use of mutex, semaphores,
I have seen the following snippet in GoogleAnalytics' Android library code: private class TrackerThread
Very simple question: suppose I have the following js/jquery code doSomething(); $.get(url, callback); doSomethingElse();
I've been following the 'Netbeans E-Commerce tutorial', and have am currently on this step:

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.