First mutex object
Createmutex (,,)
The first parameter is the security level, and null is the default value.
The second parameter is the owner, and false indicates that there is no owner.
The third parameter is a mutex object, and null is anonymous (if not anonymous, "Naming object mutex" means only one instance object can be created)
Returns a handle.
Mutex is related to the thread. There is a thread ID and a count ID internally to check the number of times referenced. The number of times releasemutex () must be called after multiple references. If the current thread has
Terminate, the mutex is automatically released, and the count is reduced by one.
Request mutex object:
Waitforsingleobject (,) // whether it is in a signal state
The first handle Parameter
Interval of the second parameter
Returned value: If the thread is terminated abnormally or no releasemutex exists before the thread is terminated, wait_abandoned is returned.
Note: handle can be the handle of the following objects:
Change Notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable Timer
Closehandle () is required after creation ()
Releasemutex ()
Second, event object
The event object is also a kernel object and can be synchronized between threads.
Createevent (,,,)
The first parameter, security level. null is the default level.
The second parameter, manual or automatic reset, true, requires manual resetevent (), flase, the system automatically reset to non-Signal Status
The third parameter specifies the event initialization status, which is true and has the Signal status, flase, and non-Signal status. (You can also call setevent () after creation to set whether the event is in the signal status)
Fourth parameter, the name of the event object
Waitforsingleobject () Here event is used as the parameter
Third keyCodeSection (critical section)
Initializecriticalsection ()
Entercriticalsection (); // determines whether the code can be obtained. If the code segment can be accessed
Leavecriticalsection (); // release the ownership after the access is completed.
Deletecriticalsection ();
Comparison between the three
The mutex object and event object belong to the kernel object. The thread synchronization using the kernel object is slow, but the kernel object such as the mutex object and event object can be used in multiple processes.
.
Key code segments (critical sections) work in the user mode, and the synchronization speed is fast. However, when using key code segments, it is easy to enter the deadlock status, because it is not possible to wait for key code segments to enter.
Set the timeout value.