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!
Notice that the producer produced three work items, but the consumer performed only two of them.
AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. If no threads are waiting, the state remains signaled indefinitely. If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block.
Yes, there certainly is a difference. A Semaphore
is used to throttle access to a resource or block of code. When WaitOne
is called a thread will block until a count from the semaphore becomes available. To make a count availabe you would call Release
. 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 the Semaphore
is that its state is manually controlled via calls to WaitOne
and Release
.
An AutoResetEvent
on the other hand is primarily used as a signaling mechanism. One thread will block via a call to WaitOne
waiting for a signal. Another thread will call Set
to 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 the AutoResetEvent
is that it is manually signaled via a call to Set
and automatically reset after when a single call to WaitOne
returns.
So here is a summary of differences:
Semaphore
's state is manually controlled.AutoResetEvent
's state is manually set, but automatically reset.Semaphore
threads typically balance the Release
and WaitOne
calls.AutoResetEvent
one thread is typically designated as the signaler and another is the waiter.Semaphore
throttles access to a resource or block of code.AutoResetEvent
signals a thread to take some action.Think of a AutoResetEvent
as 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 Semaphore
as 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.
static void Main() { var are = new AutoResetEvent(false); are.Set(); are.Set(); var semaphore = new Semaphore(0, 1); semaphore.Release(); semaphore.Release(); }
It comes as no surprise that you will get an exception on the second semaphore.Release
call whereas the second call to Set
passes through just fine. The reason is because an ARE is setting a boolean flag whereas the semaphore is attempting to increase the count.
The WaitOne
methods will work the same way, but the Release
and Set
methods 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.
public static void Main() { var latch = new AutoResetEvent(false); new Thread( () => { latch.WaitOne(); // Wait for the latch. }).Start(); latch.Set(); // Release the latch. }
Here is a scenario that can only be satisfied by a AutoResetEvent
.
static void Main() { var are = new AutoResetEvent(false); new Thread( () => { while (true) { are.WaitOne(); Console.WriteLine("go"); Thread.Sleep(2000); } }).Start(); while (true) { are.Set(); Console.WriteLine("pulse"); Thread.Sleep(1000); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With