According to Wikipedia:

Semaphores are a useful tool in the prevention of race conditions; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores and are used to implement locks.

Is a semaphore always initialized to the maximum value it can take?

Is the difference between a counting semaphore and a binary semaphore only their initial value: a semaphore is binary if and only if it is initialized to 1, and counting but not binary if and only if it is initialized to an integer larger than 1?

1 Answer 1


The difference is more about the purpose: Binary semaphores are usually used to implement mutual exclusion, whereas counting semaphores to limit access. But in the end, the binary semaphore is just a special case of the counting semaphore.

The details remain implementation dependent. In the POSIX standard for example, the semaphores are counting semaphores. So indeed, the initial value (defined with sem_init() in the case of an unnamed semaphore) would de facto define if it's binary or not. However, a counting sempahore remains a counting semaphore, and using more sem_post() (release) than sem_wait()(acquire) will increase the value of the semaphore beyond the initial value (demo). By the way, this allows also to create a semaphore in an already locked state (initial value of 0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.