AutoResetEvent 允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访问的资源。
线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号。如果 AutoResetEvent 处于非终止状态,则该线程阻塞,并等待当前控制资源的线程
通过调用 Set 发出资源可用的信号。
调用 Set 向 AutoResetEvent 发信号以释放等待线程。AutoResetEvent 将保持终止状态,直到一个正在等待的线程被释放,然后自动返回非终止状态。如果没有任何线程在等待,则状态将无限期地保持为终止状态。
可以通过将一个布尔值传递给构造函数来控制 AutoResetEvent 的初始状态,如果初始状态为终止状态,则为 true;否则为 false。
通俗的来讲只有等myResetEven.Set()成功运行后,myResetEven.WaitOne()才能够获得运行机会;Set是发信号,WaitOne是等待信号,只有发了信号,
等待的才会执行。如果不发的话,WaitOne后面的程序就永远不会执行.
可以把AutoResetEvent继承WaitHandle,只会等待信号发生。Set()方法为设置信号。 Reset()方法为取消信号。
AutoResetEvent与ManualResetEvent的区别
他们的用法\声明都很类似,Set方法将信号置为发送状态 Reset方法将信号置为不发送状态WaitOne等待信号的发送。其实,从名字就可以看出一个手动,
一个自动,这个手动和自动实际指的是在Reset方法的处理上,如下面例子:
public AutoResetEvent autoevent=new AutoResetEvent(true);
public ManualResetEvent manualevent=new ManualResetEvent(true);
默认信号都处于发送状态,
autoevent.WaitOne();
manualevent.WaitOne();
如果 某个线程调用上面该方法,则当信号处于发送状态时,该线程会得到信号,得以继续执行。差别就在调用后,autoevent.WaitOne()每次只允许一个线程
进入,当某个线程得到信号(也就是有其他线程调用了autoevent.Set()方法后)后,autoevent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只
有继续等待.也就是说,autoevent一次只唤醒一个线程。而manualevent则可以唤醒多个线程,因为当某个线程调用了set方法后,其他调用waitone的线程
获得信号得以继续执行,而manualevent不会自动将信号置为不发送.也就是说,除非手工调用了manualevent.Reset().方法,则manualevent将一直保持有信号状态,manualevent也就可以同时唤醒多个线程继续执行。如果上面的程序换成ManualResetEvent的话,就需要在waitone后面做下reset。
我们将事件的发起者(Event Source)称为Publisher,将事件的处理者(Event Handler)称为Subscriber.
AutoResetEvent 的 Set()不起作用?
今天Debug的时候发现了这个情况,后在MSDN上面查到了如下原因:
重要事项 |
---|
不能保证对 Set 方法的每次调用都释放线程。 如果两次调用十分接近,以致在线程释放之前便已发生第二次调用,则只释放一个线程。 就像第二次调用并未发生一样。 另外,如果在调用 Set 时不存在等待的线程且 AutoResetEvent 已终止,则该调用无效。 |
实验1.0: 一个线程循环的设定信号量,在主线程中循环等待并释放信号量。
static void Main(string[] args) { AutoResetEvent valueChangedEvent = new AutoResetEvent(false); int status = 0; // Create a thread to change the status value in loop. Thread thread = new Thread(() => {
Thread.Sleep(1000); for (int i = 0; i < 10; i++) { // Change the status value. status = status + 2; Debug.WriteLine("{0} Loop#{1}, ThreadId#{2}[{3}] Set value changed signal---> status: {4}", GetCurrentTime(), i, Thread.CurrentThread.ManagedThreadId, AppDomain.GetCurrentThreadId(), status); valueChangedEvent.Set(); } }); thread.Start(); while (true) { Debug.WriteLine("Start waiting for event..."); if (valueChangedEvent.WaitOne(5000)) { Debug.WriteLine("{0}, ThreadId#{1}[{2}], value changed event is received:{3}", GetCurrentTime(), Thread.CurrentThread.ManagedThreadId, AppDomain.GetCurrentThreadId(), status); } else { Debug.WriteLine("Time-out to wait for the signal."); break; } } } static string GetCurrentTime() { return DateTime.Now.ToString("HH-mm-ss.fff"); }
输出如下: 可以看到 Loop #0, #4, #5, #6, #7, #8 发生的信号量,并没有被接受到。
Start waiting for event...
17-23-07.526 Loop#0, ThreadId#11[10220] Set value changed signal---> status: 2
17-23-07.530 Loop#1, ThreadId#11[10220] Set value changed signal---> status: 4
17-23-07.530, ThreadId#10[9488], value changed event is received:4
Start waiting for event...
17-23-07.552 Loop#2, ThreadId#11[10220] Set value changed signal---> status: 6
17-23-07.573, ThreadId#10[9488], value changed event is received:6
Start waiting for event...
17-23-07.593 Loop#3, ThreadId#11[10220] Set value changed signal---> status: 8
17-23-07.614, ThreadId#10[9488], value changed event is received:8
Start waiting for event...
17-23-07.636 Loop#4, ThreadId#11[10220] Set value changed signal---> status: 10
17-23-07.658, ThreadId#10[9488], value changed event is received:10
17-23-07.679 Loop#5, ThreadId#11[10220] Set value changed signal---> status: 12
17-23-07.722 Loop#6, ThreadId#11[10220] Set value changed signal---> status: 14
17-23-07.722 Loop#7, ThreadId#11[10220] Set value changed signal---> status: 16
17-23-07.724 Loop#8, ThreadId#11[10220] Set value changed signal---> status: 18
17-23-07.725 Loop#9, ThreadId#11[10220] Set value changed signal---> status: 20
The thread '<No Name>' (0x27ec) has exited with code 0 (0x0).
Start waiting for event...
17-23-07.729, ThreadId#10[9488], value changed event is received:20
Start waiting for event...
Time-out to wait for the signal.
在两次Set()之间放入一些等待时间后,可以发现所有的Signal都被接受到了。
但是,status值不完全正确,这是因为有两个线程同时可以会读/写 status变量,从不能使status线程同步变更。
当修改Status线程发送Loop#1 Set 信号量的时候,Status 值为2, 但是当主线程接受#1信号量的时候,Status的值已经被Loop#2修改为4了。
可以理解为,当信号量发生到信号量被接受是需要一定时间的。
Thread.Sleep(200); valueChangedEvent.Set();
输出结果如下:
Start waiting for event... 17-34-55.305 Loop#0, ThreadId#11[10368] Set value changed signal---> status: 2 17-34-55.510, ThreadId#10[9088], value changed event is received:4 17-34-55.510 Loop#1, ThreadId#11[10368] Set value changed signal---> status: 4 Start waiting for event... 17-34-55.755 Loop#2, ThreadId#11[10368] Set value changed signal---> status: 6 17-34-55.755, ThreadId#10[9088], value changed event is received:6 Start waiting for event... 17-34-55.976 Loop#3, ThreadId#11[10368] Set value changed signal---> status: 8 17-34-55.976, ThreadId#10[9088], value changed event is received:8 Start waiting for event... 17-34-56.199, ThreadId#10[9088], value changed event is received:10 Start waiting for event... 17-34-56.199 Loop#4, ThreadId#11[10368] Set value changed signal---> status: 10 17-34-56.421 Loop#5, ThreadId#11[10368] Set value changed signal---> status: 12 17-34-56.421, ThreadId#10[9088], value changed event is received:12 Start waiting for event... 17-34-56.643 Loop#6, ThreadId#11[10368] Set value changed signal---> status: 14 17-34-56.643, ThreadId#10[9088], value changed event is received:14 Start waiting for event... 17-34-56.865 Loop#7, ThreadId#11[10368] Set value changed signal---> status: 16 17-34-56.865, ThreadId#10[9088], value changed event is received:16 Start waiting for event... 17-34-57.086 Loop#8, ThreadId#11[10368] Set value changed signal---> status: 18 17-34-57.086, ThreadId#10[9088], value changed event is received:18 Start waiting for event... 17-34-57.308 Loop#9, ThreadId#11[10368] Set value changed signal---> status: 20 17-34-57.308, ThreadId#10[9088], value changed event is received:20 Start waiting for event... The thread '<No Name>' (0x2880) has exited with code 0 (0x0). 17-34-57.531, ThreadId#10[9088], value changed event is received:20 Start waiting for event... Time-out to wait for the signal.
解决的方法就是用一个dictory 来保存status值
static void Main(string[] args) { AutoResetEvent valueChangedEvent = new AutoResetEvent(false); Dictionary<int, int> dictStatus = new Dictionary<int, int>(); int status = 0; // Create a thread to change the status value in loop. Thread thread = new Thread(() => { Thread.Sleep(1000); for (int i = 0; i < 10; i++) { Thread.Sleep(200); status = status + 2; dictStatus.Add(i, status); Debug.WriteLine("{0} Loop#{1}, ThreadId#{2}[{3}] Set value changed signal---> status: {4}", GetCurrentTime(), i, Thread.CurrentThread.ManagedThreadId, AppDomain.GetCurrentThreadId(), status); valueChangedEvent.Set(); } }); thread.Start(); for (int j = 0; j < 10; j++) { Debug.WriteLine("Start waiting for event..."); lock (statusLock) { if (valueChangedEvent.WaitOne(5000)) { { Debug.WriteLine("{0}, ThreadId#{1}[{2}], value changed event is received:{3}", GetCurrentTime(), Thread.CurrentThread.ManagedThreadId, AppDomain.GetCurrentThreadId(), dictStatus[j]); } } else { Debug.WriteLine("Time-out to wait for the signal."); break; } } } } static string GetCurrentTime() { return DateTime.Now.ToString("HH-mm-ss.fff"); }
输出为:
20-17-05.615 Loop#0, ThreadId#10[20860] Set value changed signal---> status: 2 20-17-05.620, ThreadId#9[17044], value changed event is received:2 Start waiting for event... 20-17-05.822 Loop#1, ThreadId#10[20860] Set value changed signal---> status: 4 20-17-05.822, ThreadId#9[17044], value changed event is received:4 Start waiting for event... 20-17-06.023 Loop#2, ThreadId#10[20860] Set value changed signal---> status: 6 20-17-06.024, ThreadId#9[17044], value changed event is received:6 Start waiting for event... 20-17-06.225 Loop#3, ThreadId#10[20860] Set value changed signal---> status: 8 20-17-06.225, ThreadId#9[17044], value changed event is received:8 Start waiting for event... 20-17-06.427 Loop#4, ThreadId#10[20860] Set value changed signal---> status: 10 20-17-06.429, ThreadId#9[17044], value changed event is received:10 Start waiting for event... 20-17-06.630 Loop#5, ThreadId#10[20860] Set value changed signal---> status: 12 20-17-06.632, ThreadId#9[17044], value changed event is received:12 Start waiting for event... 20-17-06.833 Loop#6, ThreadId#10[20860] Set value changed signal---> status: 14 20-17-06.833, ThreadId#9[17044], value changed event is received:14 Start waiting for event... 20-17-07.033 Loop#7, ThreadId#10[20860] Set value changed signal---> status: 16 20-17-07.034, ThreadId#9[17044], value changed event is received:16 Start waiting for event... 20-17-07.236 Loop#8, ThreadId#10[20860] Set value changed signal---> status: 18 20-17-07.237, ThreadId#9[17044], value changed event is received:18 Start waiting for event... 20-17-07.437 Loop#9, ThreadId#10[20860] Set value changed signal---> status: 20 The thread '<No Name>' (0x517c) has exited with code 0 (0x0). 20-17-07.439, ThreadId#9[17044], value changed event is received:20
另外目前能想到的较为稳妥的方式为:
1. 将收到的消息放到一个Queue中, 但是由于Queue不是线程安全的,可以考虑使用 ConcurentQueue().
2. 建立ACK机制,主线程收到结果后,做反应,并通知子线程。子线程在收到主线程的信号后,才可以继续下一步,例如修改Status值。
参考: http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml