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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:48:26+00:00 2026-05-23T07:48:26+00:00

Given the canonical example for condition variables pthread_mutex_lock(&count_mutex); pthread_cond_wait(&count_threshold_cv, &count_mutex); Then I can send

  • 0

Given the canonical example for condition variables

pthread_mutex_lock(&count_mutex);
pthread_cond_wait(&count_threshold_cv, &count_mutex);

Then I can send a signal from another thread, and this thread will continue.

But I fail to see whats wrong with just trying to lock a mutex, and if the lock is taken then the thread will wait for the mutex lock to be released.

Is the only problem with this approach the constant polling?

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-05-23T07:48:27+00:00Added an answer on May 23, 2026 at 7:48 am

    Let’s make this concrete. Your suggested alternative to condition variables is for the “waiter” to do this:

    loop:
        lock mutex
        check predicate
        if (predicate is false)
            unlock mutex
            sleep a bit // (is this what you had in mind?)
            goto loop
    

    And for the “signaler” to do this:

        lock mutex
        make predicate true
        unlock mutex
    

    Here, “predicate” might be “the queue is not empty”, for instance.

    There are two problems with this approach. The first is the one you identified: The constant polling is inefficient. If you imagine hundreds or thousands of threads across the whole system trying to operate this way, it would bring the system to its knees. Or your “sleep a bit” would have to be so long that the sleeps themselves would add up to annoying delays.

    The second problem is more subtle. There is no guarantee that when a thread unlocks a mutex and then locks it again, another thread waiting on that mutex will be allowed to run. (This property of a mutex is called “fairness”; a mutex that provides it is said to be “fair”. POSIX does not require mutexes to be fair.) No matter how long you sleep in the “waiter”, there is no guarantee that the “signaler” will ever get past its lock mutex call.

    Condition variables solve both of these problems.

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

Sidebar

Related Questions

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given a point P on a 'canonical' ellipse defined by axes a, b ,
Given a pointer to int , how can I obtain the actual int ?
Given any particular rectangle (x1,y1)-(x2,y2), how can I generate a random point on its
Given the following string: s = 'abcdefg*' How can I match it or any
Given a set of typical models: # Application A from django.db import models class
A canonical example of patching up an otherwise covariant class is as follows: abstract
I so often run into this pattern. Can someone please give me a canonical
How can I set default value in ActiveRecord? I see a post from Pratik
How can I take any given path in bash and convert it to it's

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.