Hoser
Hoser

Reputation: 5044

Zero permit semaphores?

I'm trying to use the first response to this question to help me on an assignment I'm working on. How exactly does a Semaphore with 0 permits work? It doesn't really seem to make much sense to me. Is it just to create an eternal wait at that semaphore? If that's the case, how can you ever have a thread 'get past' the semaphore while it's just waiting for a permit that will never be given to it?

Upvotes: 5

Views: 6025

Answers (1)

Matt Ball
Matt Ball

Reputation: 359966

Again from The Little Book of Semaphores, §2.2:

Listing 2.1: Semaphore initialization syntax

fred = Semaphore(1)

The function Semaphore is a constructor; it creates and returns a new Semaphore. The initial value of the semaphore is passed as a parameter to the constructor.

So in the author's pseduocode, 0 isn't the number of permits; it's the initial value of the semaphore. What does a value of zero mean? It's explained in the text immediately proceeding listing 2.1:

If the value is positive, then it represents the number of threads that can decrement without blocking. If it is negative, then it represents the number of threads that have blocked and are waiting. If the value is zero, it means there are no threads waiting, but if a thread tries to decrement, it will block.

(emphases added)

Upvotes: 12

Related Questions