Several ways of thread synchronization

--------------------- Sync Objects in User Mode ----------------------
- ---------1, critical section (CCriticalSection) -------------------
When multiple threads access an exclusive shared resource, you can use critical area object. The thread that owns the critical section can access the protected resource or code segment. If other threads want to access, they are suspended until the thread that owns the critical section gives up the critical section.

---------------------Kernel Objects ----------------------- ----------------------------
----------2, event (CEvent)------ ------------------------------
Event mechanism, divided into automatic reset event object and manual reset event object;
1) Automatic Reset the event object. Once triggered (with a signal, setEvent(h)), only one of the waiting threads will be able to obtain the handle h of the event object, and at the same time, the event object will be automatically set to no signal. Only when the thread finishes executing and calls setEvent(h) to trigger the event object, then other threads get it. In short, only one waiting thread can get the event object at a time, because as long as one of the threads gets the event object h, then it will automatically set h to the unsignaled state until setEvent(h) is called to trigger the event object; it ensures that a thread has exclusive access to a resource.
2) Manually reset the event object. Once triggered (with a signal, setEvent(h)), all threads waiting for this event object will become schedulable, and the CPU will randomly allocate CPU time to all waiting threads. , these threads will synchronously and randomly acquire CPU time to execute the code in the thread.
----------3, Mutex (CMutex)-------------------------------
1 ) mutex object (containing a thread ID, which can know which thread accessed it) can ensure that a thread has exclusive access to a resource.
2) If the thread ID is 0 (invalid thread ID), then the mutex will not be occupied by any thread, and he is in the triggered state;
3) If the thread ID is non-zero, then a thread has already occupied the mutex , it is in an untriggered state;
4) Unlike all other kernel objects, the thread view is assumed to wait for a mutex object that is not triggered, in this case the thread usually goes into a wait state, however, the system checks the thread for the thread that wants to acquire the mutex Whether the ID is the same as the thread ID recorded inside the mutex object, if they are the same, then the system will keep the thread in a schedulable state, even if the mutex has not been triggered.
Mutex and critical section behave exactly the same, but the difference is as follows:
1) Mutex is a kernel object, and critical section is a synchronization object in user mode, which executes faster than kernel objects;
2) Threads in different processes can access The same mutex, and you can also set the maximum time to wait.
-----------4, semaphore (CSemphore) ----------------------------
semaphore and other All kernel objects are identical, but additionally contain two 32-bit values:
one for the maximum resource count and one for the current resource count.
1) The maximum resource count indicates the maximum number of resources that the semaphore can control (this is set when the semaphore is created);
2) The current resource count indicates the current number of resources available for
the semaphore; the rules for the semaphore are as follows:
1) If the current resource count is greater than 0, then the semaphore is in a triggered state;
2) If the current resource count is equal to 0, then the signal is in a non-triggered state;
3) The system will never let the current resource count become negative;
4) The current resource The count will never be greater than the maximum resource count.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325787066&siteId=291194637