I’m going through the following article:
http://www.albahari.com/threading
and I cannot get to realize the difference between an AutoResetEvent and a Semaphore initialized with maximumCount = 1. Just to see if I’m getting things right… is there any difference in these two constructs, given that usage?
Thanks!
Yes, there certainly is a difference. A
Semaphoreis used to throttle access to a resource or block of code. WhenWaitOneis called a thread will block until a count from the semaphore becomes available. To make a count availabe you would callRelease. A semaphore with a maximum count of 1 is often called a binary semaphore. A semaphore of this nature only allows access to a resource or block code from a single thread. You could use a binary semaphore in place of a mutex or monitor. The important thing to remember about theSemaphoreis that its state is manually controlled via calls toWaitOneandRelease.An
AutoResetEventon the other hand is primarily used as a signaling mechanism. One thread will block via a call toWaitOnewaiting for a signal. Another thread will callSetto initiate that signal. An ARE publishes this signal to one and only one thread and then immediately resets the ARE to an unsignaled state. The important thing to remember about theAutoResetEventis that it is manually signaled via a call toSetand automatically reset after when a single call toWaitOnereturns.So here is a summary of differences:
Semaphore‘s state is manually controlled.AutoResetEvent‘s state is manually set, but automatically reset.Semaphorethreads typically balance theReleaseandWaitOnecalls.AutoResetEventone thread is typically designated as the signaler and another is the waiter.Semaphorethrottles access to a resource or block of code.AutoResetEventsignals a thread to take some action.Think of a
AutoResetEventas a door to a hallway. The door will allow one and only one person through the door after receiving a command to do so. Once a person goes through the door it immediately closes and waits for another command. As long as the door keeps receiving new commands the hallway is free to fill with as many people as the number of commands given.Think of a
Semaphoreas a door to the same hallway. The door will allow a certain number of people in the hallway. The door remains open until the hallway reaches its occupancy limit at which time the door closes. After someone leaves the hallway through the other side then this door opens again.Update:
Here is the simplest possible example that demonstrates that something is clearly different.
It comes as no surprise that you will get an exception on the second
semaphore.Releasecall whereas the second call toSetpasses through just fine. The reason is because an ARE is setting a boolean flag whereas the semaphore is attempting to increase the count.The
WaitOnemethods will work the same way, but theReleaseandSetmethods will not. It is for this reason that a binary semaphore is not interchangable with an ARE. However, an ARE could be interchangable with a binary semaphore in some cases.One scenario where there is overlap is in the case of a latch for a single thread.
Here is a scenario that can only be satisfied by a
AutoResetEvent.