What is the difference between Binary Semaphore & Counting Semaphore?
What will be situation to prefer one from both of them?
Edited by Ankit_Parmar
- 4 Contributors
- forum3 Replies
- 5 Views
- 6 Years Discussion Span
- comment Latest Post by Climber Ty
It depends upon how many threads are allowed to access a resource at a time. A binary semaphore allows only one, whereas a counting semaphore may only allow N accesses at a time. The counting version is not frequently used, but it may be useful for some situations.
Analogy: You are a thread. You're looking for a room in a hotel, which is the resource. Because you are not alone, there can be several people coming in and out of the hotel at unpredictable times. So, the question is, how do you make sure not to over-book the hotel? The answer is, you need a guy at the front desk keeping count of the number of people who checked in (and didn't check out yet), that's a semaphore. Of course, this requires that everyone is diligent enough not to go into a room without having checked in, and that no one forgets to check out, that's also true of threads when using semaphores.
A binary semaphore just means that the hotel has a total of 1 room. While a counting semaphore means the hotel has N rooms.
The problem is, if you only keep count of the number of people currently checked in, but you don't keep a record of who is assigned to which room, you get the problem that everytime someone checks in, you can't direct them to any particular room that is available. This is why the counting semaphore is not as useful, at least, by itself. There are some cases where it can be sufficient to just keep count (for example, when limiting the load on a system), but in many cases you need more than that (e.g., managing a resource pool), in which case you might use a counting semaphore as part of the solution, but often not.
The reason they exist, and the reason they are useful, is because semaphores are very simple under-the-hood, in technical terms, they are really just kernel-level atomic counters. The point is that they are a simple mechanism which can be used and needed to implement certain highly efficient synchronization code. But for the average user, they are not nearly as useful or used, most of the time people use mutexes and critical sections (and maybe a few other things like condition variables and memory barriers).
Great analogy mike! I find that really interesting!