locked
WaitForMultipleObjects returned before all threads signal RRS feed

  • Question

  • #include <windows.h>
    #include <stdio.h>
    
    #define THREADCOUNT 100
    
    HANDLE ghMutex; 
    
    DWORD WINAPI WriteToDatabase( LPVOID );
    
    int main( void )
    {
        HANDLE aThread[THREADCOUNT];
        DWORD ThreadID;
        int i;
    
        // Create a mutex with no initial owner
    
        ghMutex = CreateMutex( 
            NULL,              // default security attributes
            FALSE,             // initially not owned
            NULL);             // unnamed mutex
    
        if (ghMutex == NULL) 
        {
            printf("CreateMutex error: %d\n", GetLastError());
            return 1;
        }
    
        // Create worker threads
    
        for( i=0; i < THREADCOUNT; i++ )
        {
            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
    
        WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
    
        // Close thread and mutex handles
        printf("All the threads signal...\n");
        for( i=0; i < THREADCOUNT; i++ )
            CloseHandle(aThread[i]);
    
        CloseHandle(ghMutex);
    
        return 0;
    }
    
    DWORD WINAPI WriteToDatabase( LPVOID lpParam )
    { 
        // lpParam not used in this example
        UNREFERENCED_PARAMETER(lpParam);
    
        DWORD dwCount=0, dwWaitResult; 
    
        // Request ownership of mutex.
    
        while( dwCount < 20 )
        { 
            dwWaitResult = WaitForSingleObject( 
                ghMutex,    // handle to mutex
                INFINITE);  // no time-out interval
     
            switch (dwWaitResult) 
            {
                // The thread got ownership of the mutex
                case WAIT_OBJECT_0: 
                    __try { 
                        // TODO: Write to the database
                        printf("Thread %d writing to database...\n", 
                                GetCurrentThreadId());
                        dwCount++;
                    } 
    
                    __finally { 
                        // Release ownership of the mutex object
                        if (! ReleaseMutex(ghMutex)) 
                        { 
                            // Handle error.
                        } 
                    } 
                    break; 
    
                // The thread got ownership of an abandoned mutex
                // The database is in an indeterminate state
                case WAIT_ABANDONED: 
                    return FALSE; 
            }
        }
        return TRUE; 
    }


    Hi Guys,

    This is the code I got from https://msdn.microsoft.com/en-us/library/windows/desktop/ms686927(v=vs.85).aspx

    I changed to  THREADCOUNT to 100.

    After that, I found that the WaitForMultipleObjects returned before all threads signal.

    WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);

    The True means it has to wait until all threads signals... right?

    Any suggestions?

    Wednesday, November 16, 2016 11:38 PM

Answers

  • On 11/16/2016 6:38 PM, cppBeginer wrote:

    #define THREADCOUNT 100
        WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);

    WaitForMultipleObjects only accepts up to MAXIMUM_WAIT_OBJECTS at a time. That constant is defined as 64. That's why your call fails.

    • Proposed as answer by Baron Bi Thursday, November 17, 2016 1:48 AM
    • Marked as answer by cppBeginnnnnner Thursday, November 17, 2016 3:22 AM
    Wednesday, November 16, 2016 11:53 PM

All replies

  • On 11/16/2016 6:38 PM, cppBeginer wrote:

    #define THREADCOUNT 100
        WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);

    WaitForMultipleObjects only accepts up to MAXIMUM_WAIT_OBJECTS at a time. That constant is defined as 64. That's why your call fails.

    • Proposed as answer by Baron Bi Thursday, November 17, 2016 1:48 AM
    • Marked as answer by cppBeginnnnnner Thursday, November 17, 2016 3:22 AM
    Wednesday, November 16, 2016 11:53 PM
  • Thanks very much
    Thursday, November 17, 2016 3:22 AM