When it comes to Windows thread synchronization, there are cyclic locks, critical sections (critical code Snippets), and kernel objects (EVENT,SEMAPHORE,MUTEX). Even processes, thread handle, and so on.
To talk about usage:
Both critical sections and mutexes can be used to control mutually exclusive access to shared resources, where the critical section is a user-level object, consumes little and is fast, but cannot cross processes. Mutexes are kernel objects that are slow, but can span processes.
Semaphore can be used for resource control, two key parameters of the semaphore, the number of existing resources and the maximum number of resources, can be used to control the resources, through releasesemaphore to increase the resources, waitfor* function to reduce resources.
The event can be used to notify that the status of the event can be changed manually to inform all waiting threads (manual reset event) or whether a single thread (auto reset event) is running.
Tell me more about the note:
The mutex is a thread-path attribute of the mutex, and if id=0 is the notification state, the waitfor* function returns, the ID is set to the fetched Id,releasemutex after the ID is set to 0, and the other thread has the opportunity to obtain the mutex. If the thread does not exit with an ReleaseMutex exception, the mutex is also freed, the ID is set to 0, and the other threads have the opportunity to obtain the mutex.
There are two uses of event, one is auto-reset and one is manual reset. An automatic reset resets to no notification after waitfor* acquires the event, so that only one waiting thread (waitfor*) returns. A manual reset will wait until resetevent is reset to the non-notification state, so all waiting threads (waitfor*) will return.
Semaphore can control the number of concurrently running threads, the maximum number of resources determines the number of threads running at the same time, and the number of existing resources determines the number of threads that can now run. (waitfor*) To reduce resources, releasesemaphore increase the number of resources, the total number of resources does not exceed the maximum number of resources.
Windows thread Synchronization