Wait Function
/*************************************** ********************************* // Wait for a single object function waitforsingleobject () DWORD waitforsingleobject (// function waits for a single object. If the object is set to a bit, _ in handle hhandle is returned, // Number of waiting ms for the synchronization object handle _ in DWORD dwmilliseconds, if you need unlimited waiting, set it to infinite *//********************************* ***************************************/ /*************************************** * ********************************** // wait for multiple objects function waitformultipleobjects () waitformultipleobjects (DWORD ncount, // Number of synchronization objects in lphandles const handle * lphandles, // synchronize the array bool bwaitall of the object handle, // whether to wait for all objects in lphandles; if this parameter is set to true, DWORD dwmilliseconds is returned when all the slots of the object are set. If this parameter is set to false, DWORD dwmilliseconds is returned when the slot of one of the waiting objects is set) // Number of waiting Ms *//******************************* **************************************** */
Demo
/* This example demonstrates mutual synchronization between multiple threads. The main thread has created multiple event objects and then created multiple threads. Each thread has an event object. Write Data to the shared memory. Several sub-threads created by the main thread wait for the write operation of the main thread to complete the post-bit event, and then each sub-thread will read the data in the shared memory. After reading, each sub-thread positions its respective events. After the write operation is completed, the main thread waits for the event corresponding to each sub-thread to be set. If all sub-thread events have been set, it means that all sub-threads have completed the operation, and the main thread starts to write data to the shared memory. */# Include <windows. h> # include <stdio. h> # define NUMTHREADS 3 # define FOR_TIME 5 HANDLE hWriteEvents [NUMTHREADS]; HANDLE hReadEvents [NUMTHREADS]; BYTE lp1_buf [16]; // thread function, read shared memory dword winapi ThreadProc (LPVOID lParam) {int threadIndex = (int) lParam; byte lpRead [16]; for (int I = 0; I <FOR_TIME; I ++) {// waiting for the write time to be set, indicating that the data has been written to WaitForSingleObject (hWriteEvents [threadIndex], INFINITE); Sleep (rand () % 20 ); // time interval required for simulating data processing CopyMemory (lpRead, lp1_buf, 16); SetEvent (hReadEvents [threadIndex]); // set the read Event to a bit, indicates that the read operation has completed printf ("thread % u \ t % d read, content: % s \ n", threadIndex, I, lpRead ); // print read content} return 0;} // it is called by the main thread to write data to the shared memory and wait for all read threads to read the macro function to return void WriteToBuffer () {// complete FOR_TIME read/write for (int I = 0; I <FOR_TIME; I ++) {Sleep (rand () % 100 ); // The time interval required for writing wsprintf (LPTSTR) lp1_buf, "share % d", I); // write the shared memory for (int j = 0; j <NUMTHREADS; j ++) {// set the write Event corresponding to the thread to "flag", indicating that the write operation is completed. Other threads can start to read SetEvent (hWriteEvents [j]);} // wait for all threads to finish reading and start writing WaitForMultipleObjects (NUMTHREADS, hReadEvents, TRUE, INFINITE) ;}} void Init () {// create multiple threads and read shared memory, master thread write shared memory // each thread has a corresponding read/write synchronization event for (int I = 0; I <NUMTHREADS; I ++) {hWriteEvents [I] = CreateEvent (NULL, FALSE, FALSE, NULL); // It is automatically reset. The initial value is hReadEvents [I] = CreateEvent (NULL, FALSE, NULL); HANDLE hThread = CreateThread (NULL, 0, ThreadProc, (LPVOID) I, 0, NULL);} WriteToBuffer ();} int main () {Init (); system ("pause"); return 0 ;}