comparison Implab/Parallels/Signal.cs @ 128:6241bff0cd64 v2

Added Signal class a lightweight alternative to ManualResetEvent
author cin
date Thu, 29 Jan 2015 05:09:31 +0300
parents
children 471f596b2603
comparison
equal deleted inserted replaced
127:d86da8d2d4c3 128:6241bff0cd64
1 using System;
2 using System.Threading;
3
4 namespace Implab.Parallels {
5 /// <summary>
6 /// Implements simple signalling logic using <see cref="Monitor.PulseAll(object)"/>.
7 /// </summary>
8 public class Signal {
9 readonly object m_lock = new object();
10 bool m_state;
11
12 public void Set() {
13 lock(m_lock) {
14 m_state = true;
15 Monitor.PulseAll(m_lock);
16 }
17 }
18
19 public void Wait() {
20 lock (m_lock)
21 if (!m_state)
22 Monitor.Wait(m_lock);
23 }
24
25 public bool Wait(int timeout) {
26 lock (m_lock)
27 return m_state || Monitor.Wait(m_lock, timeout);
28 }
29 }
30 }
31