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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:04:55+00:00 2026-06-02T15:04:55+00:00

There are: one conditional variable var: values of var can be 0 or 1

  • 0

There are:

one conditional variable “var”: values of var can be 0 or 1 only;

one provider thread “thP”: when received data from net, set var 1 (available);

one consumer thread “thC”: wait() for var. When got var, set it 0 (unavailable), and process something afterwards.

.

I found two ways to implement this simple model:

(1) Use pthread_cond_t as var.
This method needs an additional mutex and a callback function to unlock mutex while thread will be cleaned.

(2) Use sem_t as var.
This method may cause a situation as “var>1”. thC may do more than one wait() operations to decrease the var’s value to 1.

.

Question is:

To implement this simple model “single provider, single consumer, one conditional variable”, which type should I use for var, pthread_cond_t, sem_t or simply using pthread_mutex_t as a binary semaphore?

.

Thanks very much!

  • 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-02T15:04:57+00:00Added an answer on June 2, 2026 at 3:04 pm

    For a single consumer, single provider, bounded queue example, the typical was to do this is with one lock protecting two conditional variables (one signalled when ‘queue is not full’ and one signalled when ‘queue is not empty’).

    The algorithm looks like this:

    post(item):
      lock;
      while (queue is full):
        wait(cvar_queue_not_full);
      queue.push(item)
      signal(cvar_queue_not_empty);
      unlock;
    
    consume():
      lock;
      while (queue is empty):
        wait(cvar_queue_not_empty);
      item = queue.shift(item);
      signal(cvar_queue_not_full);
      unlock;
      return item;
    

    This is essentially a bounded semaphore, but allows you to atomically adjust the semaphore and push/pop the queue at the same time. Unfortunately POSIX semaphores are unbounded and do not offer atomicity.

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

Sidebar

Related Questions

Is there a way to select data where any one of multiple conditions occur
Is there one method or property in the .NET Framework that tells if an
$0 is the variable for the top level Ruby program, but is there one
What are the best algorithms for sorting data in C#? Is there one sorting
I want to variable take one of two queries but there are two different
Is there a mechanism to have a conditional variable use multiple mutexes? I am
I'm looking for a standard dual-map structure - is there one implemented in std/boost/another
What is the difference between these three heavily related fields? Is there one specific
Network file systems are offered by Windows and (L)Unix. Is there one for IBM
There's one thing I haven't found in RFC 2616 ("Hypertext Transfer Protocol -- HTTP/1.1")

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.