Critical section
Critical section (critical section) is used to achieve "exclusive possession". Scope of application is a single process
Between the threads. It is:
A local object, not a core object.
Fast and efficient.
Not be able to have more than one critical section at the same time being waited.
Unable to detect whether a thread has been discarded.
You can implement mutual exclusion between threads and cannot be used to implement synchronization.
Semaphore
Semaphore is used to track limited resources. It is:
A core object.
No owner.
Can be named so that it can be opened by another process.
Can be freed by any one thread (released).
It can realize inter-thread mutual exclusion and synchronization between threads.
When used across processes, other processes do not receive notifications if the thread that owns the semaphore ends unexpectedly.
Mutex
A Mutex is a core object that can achieve "exclusive possession" between different threads,
Even if those threads belong to different processes. It is:
A core object.
If the thread that owns the mutex ends, it produces a "abandoned" error
Error messages.
I can be named so that it can be opened by other processes.
Can only be freed by the thread that owns it (released).
When used across processes, other processes receive a wait_abandoend message if the process that owns the mutex ends unexpectedly.
Event
The Event object is typically used for overlapped I/O, or for designing some custom
The synchronization object that is defined. It is:
A core object.
However, it is used to implement the mutex and synchronization of threads.
Can be named so that it can be opened by another process.
When used across processes, other processes will not be notified if the thread that owns the semaphore ends unexpectedly.
Note: Both the critical section and the mutex have the concept of "thread ownership", so they cannot be used for synchronization between threads and can only be used to implement mutual exclusion. The reason is that because a thread that creates a critical section or mutex can not wait for leavecriticalsection (), SetEvent () can enter the protected program section unconditionally because it has this right. In addition, the mutex can handle the "abandon" operation very well. If the thread terminates unexpectedly when the pair is not released, the other thread can wait for a wait_abandoned_0. But neither the event nor the semaphore can be done.
Both events and semaphores allow for mutual exclusion and synchronization between threads and processes.
As far as efficiency is concerned, the efficiency of the critical zone is the highest, because it is not a kernel object, while the other three are core objects, and it is relatively inefficient to use the operating system to achieve it.
However, if you are going to use the mutex, event object, and semaphore.
In short: At design time, try not to use global variables first, if you do not first then consider using Inter ... () function, and then in the critical section object, the last is the event, the mutex, the semaphore.
Several methods of multithreading synchronization mechanism