我已经阅读了关于此的文档,我想我明白了.一个AutoResetEvent
当代码经过复位event.WaitOne()
,但ManualResetEvent
没有.
它是否正确?
是.这就像收费站和门之间的区别.的ManualResetEvent
是门,其需要手动关闭(重置).这AutoResetEvent
是一个收费站,允许一辆车经过并自动关闭,然后下一个可以通过.
想象一下AutoResetEvent
执行WaitOne()
和Reset()
作为单个原子操作.
简短的回答是肯定的.最重要的区别是AutoResetEvent只允许一个等待的线程继续.另一方面,ManualResetEvent将继续允许线程(几个同时均匀)继续,直到你告诉它停止(重置它).
取自Joseph#Albahari的C#3.0 Nutshell书
C#中的线程 - 免费电子书
ManualResetEvent是AutoResetEvent的变体.它的不同之处在于它在WaitOne调用中通过线程后不会自动复位,因此像gate:call Set这样的函数会打开门,允许任何数量的线程在门口通过WaitOne; 调用Reset关闭门,可能导致服务器队列累积,直到下一次打开.
可以使用布尔"gateOpen"字段(使用volatile关键字声明)与"spin-sleeping"结合来模拟此功能 - 重复检查标志,然后在短时间内休眠.
ManualResetEvents有时用于表示特定操作已完成,或者线程已完成初始化并准备好执行工作.
我创建了简单的例子来澄清对ManualResetEvent
vs的理解AutoResetEvent
.
AutoResetEvent
:假设您有3个工作线程.如果任何这些线程将调用WaitOne()
所有其他2个线程将停止执行并等待信号.我假设他们正在使用WaitOne()
.它像是; 如果我不工作,没人会工作.在第一个例子中,你可以看到
autoReset.Set(); Thread.Sleep(1000); autoReset.Set();
当你调用Set()
所有线程将工作并等待信号.1秒钟后,我发送第二个信号,然后执行并等待(WaitOne()
).想想这些家伙是足球队员,如果有一名球员说我会等到经理给我打电话,其他人会等到经理告诉他们继续(Set()
)
public class AutoResetEventSample { private AutoResetEvent autoReset = new AutoResetEvent(false); public void RunAll() { new Thread(Worker1).Start(); new Thread(Worker2).Start(); new Thread(Worker3).Start(); autoReset.Set(); Thread.Sleep(1000); autoReset.Set(); Console.WriteLine("Main thread reached to end."); } public void Worker1() { Console.WriteLine("Entered in worker 1"); for (int i = 0; i < 5; i++) { Console.WriteLine("Worker1 is running {0}", i); Thread.Sleep(2000); autoReset.WaitOne(); } } public void Worker2() { Console.WriteLine("Entered in worker 2"); for (int i = 0; i < 5; i++) { Console.WriteLine("Worker2 is running {0}", i); Thread.Sleep(2000); autoReset.WaitOne(); } } public void Worker3() { Console.WriteLine("Entered in worker 3"); for (int i = 0; i < 5; i++) { Console.WriteLine("Worker3 is running {0}", i); Thread.Sleep(2000); autoReset.WaitOne(); } } }
在这个例子中,你可以清楚地看到,当你第一次点击Set()
它时会让所有线程都去,然后在1秒后它会发出信号让所有线程等待!一旦你再次设置它们而不管它们是否WaitOne()
在内部调用,它们将继续运行,因为你必须手动调用Reset()
以阻止所有它们.
manualReset.Set(); Thread.Sleep(1000); manualReset.Reset(); Console.WriteLine("Press to release all threads."); Console.ReadLine(); manualReset.Set();
它更多的是关于裁判/球员的关系,无论任何一名球员受伤,等待比赛的人都会继续工作.如果裁判说等待(Reset()
),则所有玩家将等到下一个信号.
public class ManualResetEventSample { private ManualResetEvent manualReset = new ManualResetEvent(false); public void RunAll() { new Thread(Worker1).Start(); new Thread(Worker2).Start(); new Thread(Worker3).Start(); manualReset.Set(); Thread.Sleep(1000); manualReset.Reset(); Console.WriteLine("Press to release all threads."); Console.ReadLine(); manualReset.Set(); Console.WriteLine("Main thread reached to end."); } public void Worker1() { Console.WriteLine("Entered in worker 1"); for (int i = 0; i < 5; i++) { Console.WriteLine("Worker1 is running {0}", i); Thread.Sleep(2000); manualReset.WaitOne(); } } public void Worker2() { Console.WriteLine("Entered in worker 2"); for (int i = 0; i < 5; i++) { Console.WriteLine("Worker2 is running {0}", i); Thread.Sleep(2000); manualReset.WaitOne(); } } public void Worker3() { Console.WriteLine("Entered in worker 3"); for (int i = 0; i < 5; i++) { Console.WriteLine("Worker3 is running {0}", i); Thread.Sleep(2000); manualReset.WaitOne(); } } }
autoResetEvent.WaitOne()
类似于
try { manualResetEvent.WaitOne(); } finally { manualResetEvent.Reset(); }
作为原子操作
好的,通常在同一个帖子中添加2个答案不是一个好习惯,但我不想编辑/删除我以前的答案,因为它可以帮助另一种方式.
现在,我在下面创建了更加全面且易于理解的"运行 - 学习"控制台应用程序片段.
只需在两个不同的控制台上运行示例,并观察行为.你会更清楚地了解幕后发生的事情.
手动重置事件
using System; using System.Threading; namespace ConsoleApplicationDotNetBasics.ThreadingExamples { public class ManualResetEventSample { private readonly ManualResetEvent _manualReset = new ManualResetEvent(false); public void RunAll() { new Thread(Worker1).Start(); new Thread(Worker2).Start(); new Thread(Worker3).Start(); Console.WriteLine("All Threads Scheduled to RUN!. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Main Thread is waiting for 15 seconds, observe 3 thread behaviour. All threads run once and stopped. Why? Because they call WaitOne() internally. They will wait until signals arrive, down below."); Thread.Sleep(15000); Console.WriteLine("1- Main will call ManualResetEvent.Set() in 5 seconds, watch out!"); Thread.Sleep(5000); _manualReset.Set(); Thread.Sleep(2000); Console.WriteLine("2- Main will call ManualResetEvent.Set() in 5 seconds, watch out!"); Thread.Sleep(5000); _manualReset.Set(); Thread.Sleep(2000); Console.WriteLine("3- Main will call ManualResetEvent.Set() in 5 seconds, watch out!"); Thread.Sleep(5000); _manualReset.Set(); Thread.Sleep(2000); Console.WriteLine("4- Main will call ManualResetEvent.Reset() in 5 seconds, watch out!"); Thread.Sleep(5000); _manualReset.Reset(); Thread.Sleep(2000); Console.WriteLine("It ran one more time. Why? Even Reset Sets the state of the event to nonsignaled (false), causing threads to block, this will initial the state, and threads will run again until they WaitOne()."); Thread.Sleep(10000); Console.WriteLine(); Console.WriteLine("This will go so on. Everytime you call Set(), ManualResetEvent will let ALL threads to run. So if you want synchronization between them, consider using AutoReset event, or simply user TPL (Task Parallel Library)."); Thread.Sleep(5000); Console.WriteLine("Main thread reached to end! ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } public void Worker1() { for (int i = 1; i <= 10; i++) { Console.WriteLine("Worker1 is running {0}/10. ThreadId: {1}.", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(5000); // this gets blocked until _autoReset gets signal _manualReset.WaitOne(); } Console.WriteLine("Worker1 is DONE. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } public void Worker2() { for (int i = 1; i <= 10; i++) { Console.WriteLine("Worker2 is running {0}/10. ThreadId: {1}.", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(5000); // this gets blocked until _autoReset gets signal _manualReset.WaitOne(); } Console.WriteLine("Worker2 is DONE. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } public void Worker3() { for (int i = 1; i <= 10; i++) { Console.WriteLine("Worker3 is running {0}/10. ThreadId: {1}.", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(5000); // this gets blocked until _autoReset gets signal _manualReset.WaitOne(); } Console.WriteLine("Worker3 is DONE. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } } }
自动重置事件
using System; using System.Threading; namespace ConsoleApplicationDotNetBasics.ThreadingExamples { public class AutoResetEventSample { private readonly AutoResetEvent _autoReset = new AutoResetEvent(false); public void RunAll() { new Thread(Worker1).Start(); new Thread(Worker2).Start(); new Thread(Worker3).Start(); Console.WriteLine("All Threads Scheduled to RUN!. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Main Thread is waiting for 15 seconds, observe 3 thread behaviour. All threads run once and stopped. Why? Because they call WaitOne() internally. They will wait until signals arrive, down below."); Thread.Sleep(15000); Console.WriteLine("1- Main will call AutoResetEvent.Set() in 5 seconds, watch out!"); Thread.Sleep(5000); _autoReset.Set(); Thread.Sleep(2000); Console.WriteLine("2- Main will call AutoResetEvent.Set() in 5 seconds, watch out!"); Thread.Sleep(5000); _autoReset.Set(); Thread.Sleep(2000); Console.WriteLine("3- Main will call AutoResetEvent.Set() in 5 seconds, watch out!"); Thread.Sleep(5000); _autoReset.Set(); Thread.Sleep(2000); Console.WriteLine("4- Main will call AutoResetEvent.Reset() in 5 seconds, watch out!"); Thread.Sleep(5000); _autoReset.Reset(); Thread.Sleep(2000); Console.WriteLine("Nothing happened. Why? Becasuse Reset Sets the state of the event to nonsignaled, causing threads to block. Since they are already blocked, it will not affect anything."); Thread.Sleep(10000); Console.WriteLine("This will go so on. Everytime you call Set(), AutoResetEvent will let another thread to run. It will make it automatically, so you do not need to worry about thread running order, unless you want it manually!"); Thread.Sleep(5000); Console.WriteLine("Main thread reached to end! ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } public void Worker1() { for (int i = 1; i <= 5; i++) { Console.WriteLine("Worker1 is running {0}/5. ThreadId: {1}.", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); // this gets blocked until _autoReset gets signal _autoReset.WaitOne(); } Console.WriteLine("Worker1 is DONE. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } public void Worker2() { for (int i = 1; i <= 5; i++) { Console.WriteLine("Worker2 is running {0}/5. ThreadId: {1}.", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); // this gets blocked until _autoReset gets signal _autoReset.WaitOne(); } Console.WriteLine("Worker2 is DONE. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } public void Worker3() { for (int i = 1; i <= 5; i++) { Console.WriteLine("Worker3 is running {0}/5. ThreadId: {1}.", i, Thread.CurrentThread.ManagedThreadId); Thread.Sleep(500); // this gets blocked until _autoReset gets signal _autoReset.WaitOne(); } Console.WriteLine("Worker3 is DONE. ThreadId: {0}", Thread.CurrentThread.ManagedThreadId); } } }
是.这绝对是正确的.
您可以将ManualResetEvent视为指示状态的方式.某些东西打开(设置)或关闭(重置).有一段时间的事件.等待该状态发生的任何线程都可以继续.
AutoResetEvent与信号更具可比性.一次性表明事情已经发生.没有任何持续时间的事件.通常但不一定发生的"某事"很小并且需要由单个线程处理 - 因此在单个线程消耗了事件之后自动重置.
恩,那就对了.
您可以通过使用这两个来获得一个想法.
如果您需要告诉您已完成某些工作,而其他(线程)等待此工作现在可以继续,则应使用ManualResetEvent.
如果您需要对任何资源进行互斥访问,则应使用AutoResetEvent.
AutoResetEvent在内存中维护一个布尔变量.如果布尔变量为false,则它会阻塞线程,如果布尔变量为true,则取消阻塞线程.
当我们实例化一个AutoResetEvent对象时,我们在构造函数中传递boolean value的默认值.下面是实例化AutoResetEvent对象的语法.
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
WaitOne方法
此方法阻塞当前线程并等待其他线程的信号.WaitOne方法将当前线程置于Sleep线程状态.如果WaitOne方法收到信号则返回true,否则返回false.
autoResetEvent.WaitOne();
WaitOne方法的第二次重载等待指定的秒数.如果它没有得到任何信号线程继续其工作.
static void ThreadMethod() { while(!autoResetEvent.WaitOne(TimeSpan.FromSeconds(2))) { Console.WriteLine("Continue"); Thread.Sleep(TimeSpan.FromSeconds(1)); } Console.WriteLine("Thread got signal"); }
我们通过传递2秒作为参数来调用WaitOne方法.在while循环中,它等待信号2秒然后继续工作.当线程得到信号时,WaitOne返回true并退出循环并打印"Thread got signal".
设定方法
AutoResetEvent Set方法将信号发送到等待线程以继续其工作.下面是调用Set方法的语法.
autoResetEvent.Set();
ManualResetEvent在内存中维护一个布尔变量.当布尔变量为false时,它会阻塞所有线程,当布尔变量为true时,它会解除阻塞所有线程.
当我们实例化ManualResetEvent时,我们使用默认的布尔值初始化它.
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
在上面的代码中,我们使用false值初始化ManualResetEvent,这意味着调用WaitOne方法的所有线程都将阻塞,直到某个线程调用Set()方法.
如果我们使用true值初始化ManualResetEvent,则调用WaitOne方法的所有线程都不会阻塞并自由进一步继续.
WaitOne方法
此方法阻塞当前线程并等待其他线程的信号.如果接收到信号则返回true,否则返回false.
下面是调用WaitOne方法的语法.
manualResetEvent.WaitOne();
在WaitOne方法的第二个重载中,我们可以指定当前线程等待信号的时间间隔.如果在内部时间内,它没有收到信号,它返回false并进入下一行方法.
下面是使用时间间隔调用WaitOne方法的语法.
bool isSignalled = manualResetEvent.WaitOne(TimeSpan.FromSeconds(5));
我们在WaitOne方法中指定了5秒.如果manualResetEvent对象在5秒内没有收到信号,则将isSignalled变量设置为false.
设置方法
此方法用于将信号发送到所有等待的线程.Set()方法将ManualResetEvent对象的boolean变量设置为true.所有等待的线程都被解锁并继续进行.
下面是调用Set()方法的语法.
manualResetEvent.Set();
重置方法
一旦我们在ManualResetEvent对象上调用Set()方法,它的布尔值保持为true.要重置值,我们可以使用Reset()方法.Reset方法将布尔值更改为false.
下面是调用Reset方法的语法.
manualResetEvent.Reset();
如果我们想多次向线程发送信号,我们必须在调用Set方法后立即调用Reset方法.