The purpose is as follows:
There are two threads A and B. A waits for an event in thread B in A loop. For example, in A network application, A is the main thread, and B is the receiving thread. A sends A request to the server,
Then, wait for the server to return A response or send the response again after timeout. When B receives the response, it notifies.
First look at the following code:
Int main () {HANDLE hMutex = CreateMutex (NULL, FALSE, NULL); WaitForSingleObject (hMutex, INFINITE); return 0 ;}
WaitForSingleObject always returns immediately. Why does WaitForSingleObject not work ??
It turns out that I understood it wrong and set a low-level error. WaitForSingleObject should have been returned immediately.
The first line creates an anonymous mutex. At the beginning, no thread has the mutex, so WaitForSingleObject (hMutex, INFINITE)
Always returns immediately. If WaitForSingleObject (hMutex, INFINITE) is called in thread B)
Previously, WaitForSingleObject (hMutex, INFINITE) in thread A will not return.
A single thread is always executed sequentially, So synchronization and mutex are not required. Synchronization and mutex always involve two or more threads.
I should have used semaphores or events ......