This may be silly, but I’m looking for so-called ‘real life’ examples of software which uses a semaphore with a count or more than 1.
In other words, no bouncers, librarians or toilet examples please.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I think I’ve used something like this for a “read/write” lock. That is, I want to allow up to N simultaneous readers, and only one writer at a time when there are no active readers.
So any reading thread locks the semaphore and increases the read count. If the read count is smaller than N the lock succeeds and the reader may continue. If not, the lock causes the reader thread to sched_yield.
If I want to lock for writing, I set the count to N so that no more readers can lock – e.g. they all start sleeping when they try locking. The writer thread keeps trying to lock the read semaphore. Every time a reader releases, the writer increments N again, until there are no more readers.
Then the write lock succeeds, the write operation happens, and the read semaphore is set back to zero so that reader threads may continue.