[Windows] thread discussion -- event and mutex for. Net Thread Synchronization

Source: Internet
Author: User
Tags net thread

This series is intended to record windwos thread-related knowledge points, including thread basics, thread scheduling, thread synchronization, TLS, thread pool, etc.

This topic describes event objects and mutex objects related to Kernel Object synchronization.

 

Autoresetevent and manualresetevent

There are two types of synchronization events: autoresetevent and manualresetevent. Notifications between main user threads are synchronized. Either way, each event object is actually associated with an event kernel object. In [windows] thread mantalk-thread synchronization wait function and event kernel object, the event Kernel Object and wait function are described in detail .. Net. The following is a brief review of the event kernel object:

The event kernel object has two statuses: trigger status and non-trigger status. The wait function is used to detect the status of a certain kernel object. When the kernel object detected by the lock is in the trigger status, the wait function will return directly, otherwise it will be blocked. (This statement is not complete. It is just a simple description of the general scenario ). The thread can change the event object to the trigger state through the set method. If a thread is waiting for the object to be triggered, it will receive the "event notification" and return it. The event Kernel Object of autoreset is automatically returned to the non-trigger state after it is set to the trigger state. The event object of manualreset is not automatically reset to the trigger state.

There are two versions of. Net encapsulation, automatic resetting and manual resetting. They are actually the same object, but the behavior is slightly different. When initializing an event object, you can specify the initialization status of the event object:

Static autoresetevent myresetevent = new autoresetevent (false); // The Initialization is not trigger.

Call the set method to set the event object to the trigger state:

 
Event_1.set ();

Call the reset method to set the event object to a non-trigger state:

 
Event_1.reset ();

Call waitone, waitany, or waitall to make the thread wait for the event object. Their usage can be as the name suggests.

Next we will compare the event objects encapsulated by. Net with the event objects of Windows native. For more information about the event kernel objects of Windows native, see [windows] thread mantalk-thread synchronization wait function and event kernel object.

. Net Windows API Description
Autoresetevent Createevent (bmanualreset = false)  
Manualresetevent Createevent (bmanualreset = true)  
. Set Setevent  
. Reset Resetevent  
Autoresetevent Pulseevent . Net does not provide pulseevent. For event objects, use autoresetevent instead.
-- Named The event object in. Net does not seem to be named, that is, the event object cannot span processes. However, Windows native event objects can be named like other kernel objects, so they can be shared across processes.
Eventwaithandle. waitone Waitforsingleobject . Net defines the wait series functions in the basic class eventwaithandle, encapsulates the wait Function
Eventwaithandle. waitall Waitformultipleobjects
(Bwaitall = true)
 
Eventwaithandle. waitany Waitformultipleobjects
(Bwaitall = false)
 

 

Mutex

. Net orders mutex and Win32 orders mutex are the same thing, so it can be understood from the Win32 mutex kernel object. Like common kernel objects, mutual exclusion tools can be triggered or not triggered. When an object is in the trigger state, wait for its thread to gain access and set it to the trigger State. If the object is in the trigger state, wait for its thread to suspend. Similar to monitor and lock, mutex also records the ID of the thread to which it belongs. It uses count and recursive count. Therefore, the same thread can obtain the access permission of the same mutex multiple times, however, you must ensure that the number of releases is the same.

Create a mutex object using the constructor

 
Private Static mutex mut = new mutex ();

In addition to constructors without parameters, there are also some optional parameters used to create a mutex object: whether the created thread can be initialized has the access permission of mutex immediately, and can be named by mutex, the named mutex can be visible to other processes, not just the threads of the current process. The names of msdn are described as follows:

On the server that runs the terminal service, the named system mutex can have two levels of visibility. If the name is prefixed with "Global \", mutex is visible in all Terminal Server sessions. If the name is prefixed with "Local \", mutex is only visible in the terminal server session where it is created. In this case, each other Terminal Server session on the server can have an independent mutex with the same name. If you do not specify a prefix when creating a named mutex, it uses the prefix "Local \". In a Terminal Server session, only two mutex with different name prefixes are independent mutex, which are visible to all processes in the terminal server session. That is, the prefix names "Global \" and "Local \" indicate the scope of the mutex name relative to the terminal server session rather than the process.

Access mutex through mutex. Wait Functions

 
Mut. waitone ();

Use mutex. releasemutex to release mutex

 
Mut. releasemutex ();

Before meArticle[Windows] thread mantalk-The semaphores and mutex of thread synchronization details the mutex objects under Win32 and compares the two APIs.

. Net Windows API Description
Mutex Createmutex  
Mutex Openmutex  
. Releasemutex Releasemutex  
Mutex (bool, string) Named You can name a mutex object in. net.
Eventwaithandle. waitone Waitforsingleobject . Net defines the wait series functions in the basic class eventwaithandle, encapsulates the wait Function
Eventwaithandle. waitall Waitformultipleobjects
(Bwaitall = true)
 
Eventwaithandle. waitany Waitformultipleobjects
(Bwaitall = false)
 

Labor fruit, reproduced please indicate the source: http://www.cnblogs.com/P_Chou/archive/2012/08/19/event-and-mutex-in-net-thread-sync.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.