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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:14:32+00:00 2026-06-12T14:14:32+00:00

The question is about a post regarding conditional variable that i glanced over. condition

  • 0

The question is about a post regarding conditional variable that i glanced over. condition variable

The author first give a bugged example:

/* in thread 1 */
pthread_mutex_lock(mx); 
if (state == GOOD) {
    pthread_mutex_unlock(mx);  // Here !
    wait_for_event();
    pthread_mutex_lock(mx);
}
pthread_mutex_unlock(mx);


/* in thread 2 */
pthread_mutex_lock(mx);
state = GOOD;
pthread_mutex_unlock(mx);
signal_event(); /* expecting to wake thread 1 up */

and explains as follows:

‘This pseudocode sample carries a bug. What happens if scheduler decides to switch context from thread 1 to thread 2 after pthread_mutex_unlock(mx), but before wait_for_event(). In this case, thread 2 will not wake thread 1 and thread 1 will continue sleeping, possibly forever.’

I know how conditional variable should be used, as author demonstrated in same post later.

I can see that in this bugged example, the ‘state == GOOD’ judgement and ‘wait_for_event()’ is NOT locked as a whole by a mutex. And if thread 1 is context switched right after the first ‘pthread_mutex_unlock(mx);‘, thread 2 can change ‘state’ to something else (BAD?), and signal to wake up thread 1 to proceed in the ‘state == GOOD’ logic, which i think is wrong.

But why author says ‘In this case, thread 2 will not wake thread 1 and thread 1 will continue sleeping, possibly forever.’?

Isn’t ‘signal_event();’ still called in thread 2? Was my understanding correct at all?

  • 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-12T14:14:33+00:00Added an answer on June 12, 2026 at 2:14 pm

    The bug is caused by the semantics of signal_event() and wait_for_event(). If signal_event() is called when no one is stuck in wait_for_event(), the signal is lost.

    Besides a context switch, the same problem occurs if thread 2 runs fast and thread 1 is slow. In that case, the time in thread 1 between

    pthread_mutex_unlock(mx);
    

    and

    wait_for_event();
    

    could be when thread2 does all its operations, sending a signal into oblivion (because no one is waiting for it). Then thread 1 waits, and it will never get the signal (unless thread 2 runs again for some reason).

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

Sidebar

Related Questions

I have been asked to post a new question about how to correctly sort
Question about assignments and variables (* For example *) SP = SparseArray[{},5] or SP
Not sure where to post this question, but I am concerned about the security
Just when I was about to post this question, I came up with an
Thanks to this post (referenced in this question) regarding the specification of phonetics, I'm
Really simple question about C++ constness. So I was reading this post , then
i need a quick hint regarding the following exercise question: Write a program that
Question about subclassing in matlab, under the new class system. I've got class A
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
Question about logics here: What's the most elegant way to make the menu appear/disappear

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.