andrew4582

Countdown

Apr 22nd, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.49 KB | None | 0 0
  1. public class Countdown
  2. {
  3.   object _locker = new object ();
  4.   int _value;
  5.  
  6.   public Countdown() { }
  7.   public Countdown (int initialCount) { _value = initialCount; }
  8.  
  9.   public void Signal() { AddCount (-1); }
  10.  
  11.   public void AddCount (int amount)
  12.   {
  13.     lock (_locker)
  14.     {
  15.       _value += amount;
  16.       if (_value <= 0) Monitor.PulseAll (_locker);
  17.     }
  18.   }
  19.  
  20.   public void Wait()
  21.   {
  22.     lock (_locker)
  23.       while (_value > 0)
  24.         Monitor.Wait (_locker);
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment