What does AutoResetEvent.Set () do?

If I do this:

private static System.Threading.AutoResetEvent event_2 = new System.Threading.AutoResetEvent(false); 

And then in the main thread, I do:

 event_2.Set(); 

Changes state from false to true ?

If so, basically this:

 AutoResetEventState = !AutoResetEventState 

?

+5
source share
5 answers

It sets a state that allows threads to continue execution if they Wait() on it.

If there are already any threads, then one of them will be available, and the state will be immediately established so that it is not installed, so all other threads will continue to be blocked.

If there are currently no draw streams, then the first one to wait will be immediately allowed, but subsequent streams will be blocked.

The same general mechanism is shared by other EventWaitHandle classes, but the automatic thread-reset that is allowed to run is different from ManualResetEvent , hence the names.

The initial state is signaled (allows threads) if true is passed to the constructor and not signaled if false passed, so true is the same as if you called Set() immediately after building when passing false , on the contrary, it coincides with what you called Reset() .

+5
source

A thread waits for a signal by calling WaitOne in an AutoResetEvent. If AutoResetEvent is in the no-signal state, the thread blocks, waiting for the thread that is currently managing the resource to signal that the resource is available by calling Set.

Call Set Alarms AutoResetEvent to release the pending stream. AutoResetEvent remains a signal until one waiting thread is released, and then automatically returns to a non-signaling state. If the threads do not wait, the state remains undefined.

+2
source

To add to the other answers, the reason you need this is (instead of having a bool property that you just switch):

  • Signaling: threads blocked in e.WaitOne() will be signaled and one of them will continue. If you want to do this yourself without synchronization primitives, you will need to do some kind of survey; The "blocking" thread should periodically poll the bool field (or, say, int ) to check if it has changed and is allowed to continue. If nothing else, it will unnecessarily consume processor cycles and will have a delay (depending on your polling interval).

  • Atomicity: if several threads are waiting, you have a guarantee only one will be unlocked. To ensure the same behavior using the above survey, you will need to use locks or atomic instructions (for example, those found in the Interlocked class) and a good understanding of possible reordering / memory errors of the processor and processor.

If you're looking for an easy way to synchronize multiple threads, this is one of the easiest (if not the easiest) solutions in .NET. Creating a producer / consumer queue is remarkably simple:

 // Simplified example. Check http://www.albahari.com/threading/part2.aspx // for detailed explanations of this and other syncronizing constructs private readonly AutoResetEvent _signal = new AutoResetEvent(false); private readonly ConcurrentQueue<Something> _queue = new ConcurrentQueue<Something>(); // this method can be called by one or more threads simultaneously // (although the order of enqueued items cannot be known if many threads are competing) void ProduceItem(Something s) { _queue.Enqueue(s); // enqueue item for processing _signal.Set(); // signal the consumer thread if it waiting } // this loop should be running on a separate thread. void ConsumerLoop() { while (!_ending) { // block until producer signals us _signal.WaitOne(); // process whatever is enqueued Something s = null; while (!_ending && _concurrentQueue.TryDequeue(out s)) { Process(s); } } } 

One thing you should keep in mind is that several consecutive Set calls will not necessarily signal WaitOne several times. In this example, several manufacturers can run the Set method, but it may take several milliseconds until the context switch happens and ConsumerLoop continues, which means that only one Set will process.

+1
source

Another thread is waiting for an event using event_2.Wait (), and you call .Set () from the thread, the waiting thread will continue to execute.

0
source

If so, basically this:

AutoResetEventState =! AutoResetEventState

?

In addition to this, it also sets the EventWaitHandle state to an alarm state, allowing one or more threads to continue.

0
source

Source: https://habr.com/ru/post/1233344/


All Articles