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:
- A
Semaphore's state is manually controlled.
- A
AutoResetEvent's state is manually set, but automatically reset.
- With a
Semaphore threads typically balance the Release and WaitOne calls.
- With a
AutoResetEvent one thread is typically designated as the signaler and another is the waiter.
- A
Semaphore throttles access to a resource or block of code.
- A
AutoResetEvent signals a thread to take some action.
Think of a AutoResetEvent as a door to a hallway. The door wil
answered 2011-06-26T01:06:47.167