A question about the use of waitformultipleobjects in winapi.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.