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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:26:09+00:00 2026-05-25T18:26:09+00:00

In the man page it appears that even if you initialise a semaphore to

  • 0

In the man page it appears that even if you initialise a semaphore to a value of one:

sem_init(&mySem, 0, 1);

It could still be incremented to a value greater than 1 with multiple calls to

sem_post(&mySem);

But in this code example the comment seems to think differently:

sem_init(&mutex, 0, 1);      /* initialize mutex to 1 - binary semaphore */

Is it possible to initialise a strictly binary semaphore in C?

Note: The reason for doing this instead of using a mutex in this case is the sem_post and sem_wait may be called by different threads.

  • 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-25T18:26:10+00:00Added an answer on May 25, 2026 at 6:26 pm

    If you want a strictly binary semaphore on Linux, I suggest building one out of mutexes and condition variables.

    struct binary_semaphore {
        pthread_mutex_t mutex;
        pthread_cond_t cvar;
        bool v;
    };
    
    void mysem_post(struct binary_semaphore *p)
    {
        pthread_mutex_lock(&p->mutex);
        if (p->v)
            abort(); // error
        p->v = true;
        pthread_cond_signal(&p->cvar);
        pthread_mutex_unlock(&p->mutex);
    }
    
    void mysem_wait(struct binary_semaphore *p)
    {
        pthread_mutex_lock(&p->mutex);
        while (!p->v)
            pthread_cond_wait(&p->cvar, &p->mutex);
        p->v = false;
        pthread_mutex_unlock(&p->mutex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Even though the linux man page for wait 1 explains very well that you
From the XDrawString man page it seems that it aceepts signed 32 bit x
After reading the mkdir(2) man page for the Unix system call with that name,
From epoll's man page: epoll is a variant of poll(2) that can be used
From the recv(2) man page: MSG_WAITALL This flag requests that the operation block until
I've been trying to figure this one out by reading the git-svn man-page but
According to the man page, pthread_cond_broadcast wakes up all the threads that are waiting
The recv() library function man page mention that: It returns the number of bytes
In the execve() man page it is said that argv is a list of
The man page for nice says "nice() adds inc to the nice value for

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.