Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ threads
Hello;
 
I try to create threads, and after all thread finished the program tell me "All thread finish". I use this code
#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 11-Apr-12 6:17am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think this is due to:
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:
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.
  Permalink  
Comments
windyl3ig at 11-Apr-12 12:34pm
   
oh, that is TRUE - -", you are right.
 
very thank you ^^
 
a parameter curse for an hour - -"
Code-o-mat at 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.
SAKryukov at 11-Apr-12 12:48pm
   
My 5.
--SA
Code-o-mat at 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)

  Print Answers RSS
0 OriginalGriff 299
1 Maciej Los 295
2 Sergey Alexandrovich Kryukov 230
3 Aajmot Sk 191
4 Sinisa Hajnal 166
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 11 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100