I have read the documentation on this and I think I understand. An AutoResetEvent
resets when the code passes through event.WaitOne()
, but a ManualResetEvent
does not.
Is this correct?
I have read the documentation on this and I think I understand. An Is this correct? |
||||
|
Yes. It's like the difference between a tollbooth and a door. The |
|||||||||||||||||||||
|
Just imagine that the |
||||
|
The short answer is yes. The most important difference is that an AutoResetEvent will only allow one single waiting thread to continue. A ManualResetEvent on the other hand will keep allowing threads, several at the same time even, to continue until you tell it to stop (Reset it). |
|||
|
A ManualResetEvent is a variation on AutoResetEvent. It differs in that it doesn't automatically reset after a thread is let through on a WaitOne call, and so functions like a gate: calling Set opens the gate, allowing any number of threads that WaitOne at the gate through; calling Reset closes the gate, causing, potentially, a queue of waiters to accumulate until its next opened. One could simulate this functionality with a boolean "gateOpen" field (declared with the volatile keyword) in combination with "spin-sleeping" – repeatedly checking the flag, and then sleeping for a short period of time. ManualResetEvents are sometimes used to signal that a particular operation is complete, or that a thread's completed initialization and is ready to perform work. |
|||
|
Yes, thats right. You can get an idea by the usage of these two. If you need to tell that you are finished with some work and other (threads) waiting for this can now proceed, you should use ManualResetEvent. If you need to have mutual exclusive access to any resource, you should use AutoResetEvent. |
|||
|
I created simple examples to clarify understanding of ManualResetEvent vs AutoResetEvent. AutoResetEvent: lets assume you have 3 workers thread. If any of those threads will call WaitOne() all other 2 threads will stop execution and wait for signal. I am assuming they are using WaitOne(). It is like; if I do not work, nobody works. In first example you can see that
When you call Set(); all threads will work and wait for signal. After 1 sec I am sending second signal and they execute and wait (WaitOne();). Think about these guys are soccer team players and if one player says I will wait until manager calls me, and others will wait until manager tells them to continue (Set();)
In this example you can clearly see that when you first hit Set(); it will let all threads go, then after 1 sec it signals all threads to wait! As soon as you set them again regardless they are calling WaitOne() inside, they will keep running because you have to manually call Reset() to stop them all.
It is more about Referee/Players relationship there regardless of any of the player is injured and wait for playing others will continue to work. If Referee says wait (Reset();) then all players will wait until next signal.
|
|||
|
Yes. This is absolutely correct. You could see ManualResetEvent as a way to indicate state. Something is on (Set) or off (Reset). An occurrence with some duration. Any thread waiting for that state to happen can proceed. An AutoResetEvent is more comparable to a signal. A one shot indication that something has happened. An occurrence without any duration. Typically but not necessarily the "something" that has happened is small and needs to be handled by a single thread - hence the automatic reset after a single thread have consumed the event. |
|||
|
is equal to
|
|||
|