Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello;

I try to create threads, and after all thread finished the program tell me "All thread finish". I use this code
C++
#include <stdio.h>
#include <windows.h>
#include <process.h> 
#include <mmsystem.h>
#include <fstream>
#include <iostream>

#define EVENT_NUM 2

HANDLE ghEvents[EVENT_NUM];
DWORD WINAPI ThreadRunAtCoreX( int arg );

int main()
{
	HANDLE hThread; 
    DWORD i, dwEvent, ThreadIDCore1, ThreadIDCore2, ThreadIDCore3, ThreadIDCore4; 
	for (i = 0; i < EVENT_NUM; i++) 
    { 
        ghEvents[i] = CreateEvent(	NULL,   // default security attributes
					TRUE,  // auto-reset event object
					FALSE,  // initial state is nonsignaled
					NULL);  
        if (ghEvents[i] == NULL) 
        { 
            printf("CreateEvent error: %d\n", GetLastError() ); 
            ExitProcess(0); 
        } 
		else
		{
			printf("Create ghEvents[%d] successed\n",i);
		}
    }

	// Create a thread
    hThread = CreateThread( 
                 NULL,         // default security attributes
                 0,            // default stack size
                 (LPTHREAD_START_ROUTINE) ThreadRunAtCoreX, 
                 (LPVOID)0,         // thread function arguments                
                 0,            // default creation flags
                 &ThreadIDCore1); // receive thread identifier
    if( hThread == NULL )
    {
        printf("CreateThread error: %d\n", GetLastError());
        return 1;
    }
	// Create a thread
    hThread = CreateThread( 
                 NULL,         // default security attributes
                 0,            // default stack size
                 (LPTHREAD_START_ROUTINE) ThreadRunAtCoreX, 
                 (LPVOID)1,         // thread function arguments declare as Core code
                 0,            // default creation flags
                 &ThreadIDCore2); // receive thread identifier
    if( hThread == NULL )
    {
        printf("CreateThread error: %d\n", GetLastError());
        return 1;
    }

	dwEvent = WaitForMultipleObjects( 
        EVENT_NUM,           // number of objects in array
        ghEvents,     // array of objects
        FALSE,       // wait for any object
        INFINITE);       // INFINITE wait

    // Close event handles
    for (i = 0; i < EVENT_NUM; i++) 
        CloseHandle(ghEvents[i]); 

	printf("All thread finish\n");
	getchar();	
	return 0;
}

DWORD WINAPI ThreadRunAtCoreX( int arg )
{
	for(int i =0;i<10;i++)
	printf("Argument : %d\n",arg);

    if ( !SetEvent(ghEvents[arg]) ) 
    {
        printf("SetEvent failed (%d) | arg : %d\n", GetLastError(),arg);
        return 1;
    }
	else{
		printf("SetEvent Success arg = %d\n", arg);
	}
    return 0;
}



but I get "All thread finish" massage before all thread finish and SetEvent failed (6); that means Event invalid.

my question is : why I get and error and how can I solve it?

very thank you for all answer.
Posted

1 solution

I think this is due to:
VB
dwEvent = WaitForMultipleObjects(
    EVENT_NUM,           // number of objects in array
    ghEvents,     // array of objects
    FALSE,       // wait for any object (*)
    INFINITE);       // INFINITE wait

(*) Since you specify here that it shouldn't wait for all the events to become signalled it will return as soon as any of the events got signalled.
Then here:
C#
for (i = 0; i < EVENT_NUM; i++)
    CloseHandle(ghEvents[i]);

You close all (both) events. So what probably happens is something like this:
1. You start thread A.
2. You start thread B.
3. Thread A finishes, signals event 0.
4. You wait for any of 'event 0' or 'event 1' to get signalled, 'event 0' IS signalled by 'thread A', you go on
5. you close both 'event 0' and 'event 1'.
6. 'Thread B' gets to the point where it tries to signal 'event 1', since your "main thread" already destroyed this event, ERROR.

Try changing that FALSE to TRUE in the WaitForMultipleObjects call. Thread synchronization can be a pain in the you know what.
 
Share this answer
 
Comments
windyl3ig 11-Apr-12 12:34pm    
oh, that is TRUE - -", you are right.

very thank you ^^

a parameter curse for an hour - -"
Code-o-mat 11-Apr-12 12:44pm    
Yourwelcome. By the way, if you want to wait for a thread's completion you can wait on the thread's handle the same way you would wait on an event, it will get signalled when the thread exits.
Sergey Alexandrovich Kryukov 11-Apr-12 12:48pm    
My 5.
--SA
Code-o-mat 11-Apr-12 12:58pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900