This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17115|Reply: 0

[Communication] Detailed explanation of AutoResetEvent in .net/c#

[Copy link]
Posted on 3/20/2018 1:19:01 PM | | |

thread-related concepts
1. Thread synchronization
ManualResetEvent
The Set() method sets the state to Signaled
Reset() to set it to no signal
WaitOne() will block until it has a signal, and if there is a signal at the time of calling WaitOne, it will not block
AutoResetEvent
The difference from ManualResetEvent is that AutoResetEvent.WaitOne() automatically changes the state of the event object, that is, the state of the event changes every time AutoResetEvent.WaitOne() is executed. Signal - > no signal; No signal – > has a signal
example:


  
myResetEven.Set(), which is actually equivalent to a switch, if the set() method is not executed, the following waitOne() will not be able to wait for the notification to let it execute, so that the statement after waitOne will not be executed.
Monitor method
The Wait() Pulse() mechanism is used for interthread interaction, when an object emits Monitor.Wait(), the thread that is accessing the object will remain in the waiting state. until it gets a wake-up signal. Monitor.Pulse() is used to send signals to the waiting thread.
That is to say, in the critical area between Monitor.Enter() and Monitor.Exit(), to release the lock on the object and block the current thread, it is in a waiting state in the queue.

Lock is different from Mutex
Lock synchronization between multiple threads
Synchronization of multiple threads between Mutex processes, or synchronization of multiple processes
In .Net multithreaded programming, the AutoResetEvent and ManualResetEvent classes are often used, and their usage is very similar, but there are differences. The Set method puts the signal to the send state, the Reset method puts the signal to the non-send state, and WaitOne waits for the signal to be sent. The initial state of the constructor can be determined by the parameter value, if it is true, it is not blocking, and false is the blocking state. If a thread calls the WaitOne method, the thread gets the signal when it is in the send state and continues to execute downwards. The difference is that after the call, AutoResetEvent.WaitOne() only allows one thread to enter at a time, and when a thread gets a signal, AutoResetEvent will automatically put the signal to no send state, and the other threads calling WaitOne can only continue to wait. That is, AutoResetEvent wakes up only one thread at a time; ManualResetEvent can wake up multiple threads, because when one thread calls the ManualResetEvent.Set() method, the other threads calling WaitOne get the signal to continue execution, and the ManualResetEvent does not automatically set the signal to not send. In other words, unless the ManualResetEvent.Reset() method is called manually, the ManualResetEvent will remain signaled and the ManualResetEvent can wake up multiple threads at the same time to continue execution.




Previous:.har file parsing tool and HARViewer
Next:Zookeeper distributed lock implementation
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com