Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight alternative to Manual/AutoResetEvent in C#

I have written what I hope is a lightweight alternative to using the ManualResetEvent and AutoResetEvent classes in C#/.NET. The reasoning behind this was to have Event like functionality without the weight of using a kernel locking object.

Although the code seems to work well in both testing and production, getting this kind of thing right for all possibilities can be a fraught undertaking and I would humbly request any constructive comments and or criticism from the StackOverflow crowd on this. Hopefully (after review) this will be useful to others.

Usage should be similar to the Manual/AutoResetEvent classes with Notify() used for Set().

Here goes:

using System;
using System.Threading;

public class Signal
{
  private readonly object _lock = new object();
  private readonly bool _autoResetSignal;
  private bool _notified;

  public Signal()
    : this(false, false)
  {
  }

  public Signal(bool initialState, bool autoReset)
  {
    _autoResetSignal = autoReset;
    _notified = initialState;
  }

  public virtual void Notify()
  {
    lock (_lock)
    {
      // first time?
      if (!_notified)
      {
        // set the flag
        _notified = true;

        // unblock a thread which is waiting on this signal 
        Monitor.Pulse(_lock);
      }
    }
  }

  public void Wait()
  {
    Wait(Timeout.Infinite);
  }

  public virtual bool Wait(int milliseconds)
  {
    lock (_lock)
    {
      bool ret = true;
      // this check needs to be inside the lock otherwise you can get nailed
      // with a race condition where the notify thread sets the flag AFTER 
      // the waiting thread has checked it and acquires the lock and does the 
      // pulse before the Monitor.Wait below - when this happens the caller
      // will wait forever as he "just missed" the only pulse which is ever 
      // going to happen 
      if (!_notified)
      {
        ret = Monitor.Wait(_lock, milliseconds);
      }

      if (_autoResetSignal)
      {
        _notified = false;
      }
      return (ret);
    }
  }
}
like image 470
sweetlilmre Avatar asked May 12 '10 07:05

sweetlilmre


1 Answers

This works from the assumption that Win32 events are expensive. They are not, there's little that I can think of that's cheaper than an event. A major hint that this is so is the .NET designers deciding that it would be a good idea to use a Win32 event to implement MRE and ARE.

The true cost of your replacement is the major FUD you'll experience when you've got a threading race and don't know what causes it.

like image 162
Hans Passant Avatar answered Oct 12 '22 16:10

Hans Passant