Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between an AutoResetEvent and a Semaphore with maximumCount = 1?

Tags:

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!

like image 204
brAzzi64 Avatar asked Jun 26 '11 00:06

brAzzi64


People also ask

Is AutoResetEvent a semaphore?

Notice that the producer produced three work items, but the consumer performed only two of them.

What is AutoResetEvent?

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.


1 Answers

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 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);     }  } 
like image 157
Brian Gideon Avatar answered Sep 30 '22 13:09

Brian Gideon