First, a usableProgram, Waitformultipleobjects will continue after the ready-made ends. [This example from the msdn official website transformation http://msdn.microsoft.com/en-us/library/ms686927 (V = vs.85). aspx]
# Include <windows. h> # Include <Stdio. h> # Define Threadcount 4 DWORD winapi writetodatabase (lpvoid ); Int Main ( Void ) {Handle athread [threadcount]; Int I; // Create worker threads For (I = 0 ; I <threadcount; I ++ ) {DWORD threadid; athread [I] = Createthread (null, // Default security attributes 0 , // Default stack size (Lpthread_start_routine) writetodatabase, null, // No thread function arguments 0 , // Default creation flags & Threadid ); // Receive thread identifier If (Athread [I] = Null) {printf ( " Createthread error: % d \ n " , Getlasterror ()); Return 1 ;}} // Wait for all threads to terminate Printf ( " ~~ Waitformultipleobjects start... \ n " ); Waitformultipleobjects (threadcount, athread, true, infinite); printf ( " ~~ Waitformultipleobjects end... \ n " ); // Close thread and mutex handles For (I = 0 ; I <threadcount; I ++ ) Closehandle (athread [I]); Return 0 ;} DWORD winapi writetodatabase (lpvoid lpparam ){ // Lpparam not used in this example Unreferenced_parameter (lpparam ); // Request ownership of mutex. Printf ( " Thread % d writing to database... \ n " , Getcurrentthreadid (); sleep ( 1500 ); Return True ;}
Change the threadcount thread count to 100 and run it again. The thread stops immediately.
Change the number of threadcount threads to 50 and run again. Normally, wait until the thread ends.
I don't know what the problem is...
If there are more than one thread, it will become invalid. For now, use: http://msdn.microsoft.com/en-us/library/ms687055 (V = vs.85). aspx waiting for multiple objects (Windows) to create an event to wait. Maybe it won't work if there are more than one event. I am confused about it.