The investigations over the past few days have finally understood that mutual exclusion and synchronization are not a concept.
Mutex is used to prevent multiple threads from accessing the same object, resulting in inconsistent object states.
Synchronization aims to control the execution sequence of each thread.
Windows provides the following mechanisms for mutual exclusion and synchronization: critical session, mutex, event, and semaphore ).
Among them, the critical section can only be used in the same process, so it has a high effect. The other three objects are Windows kernel objects that can be used across processes.
Use critical session and mutex to achieve mutual exclusion between threads. The critical session and mutex are basically the same. The difference is that the critical session can only be in the same process, so the speed is fast and the mutex speed is slow, but it can be cross-process.
Use event to synchronize threads.
Use semaphore to coordinate threads.
The difference between semaphore and mutex (or critical session) Is that mutex is mainly used for mutual expansion, and semaphore implements the counting mechanism, which is mainly used to coordinate resource access.