WaitForMultipleObjects

I am creating threads and each thread is writing something in a file, which a shared resource. I want all threads to finish the callback function, which is filewrite and then close.

For that, I am using WaitForMultipleObjects to wait for all threads to finish. But it never waits. This program creates all handles and immediately closes them. If I add Sleep(2500), then the threads seem to write in the file. Not sure what's going on?

Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(int i=0; i<max_threads; ++i)
	{
	    hThreadArray[i] = CreateThread( 
            NULL,                   // default security attributes
            0,                      // use default stack size  
            fileWrite,       // thread function name
            0,          // argument to thread function 
            0,                      // use default creation flags 
            &dwThreadId);   // returns the thread identifier
	}

	unsigned long temp=WaitForMultipleObjects(max_threads, hThreadArray, TRUE, INFINITE);
	printf("temp=%d\n", temp);

	//Sleep(2500);

	for(int i=0; i<max_threads; ++i)
	{
		CloseHandle(hThreadArray[i]);
		//delete hThreadArray[i];
	}
Could you post the complete program?
I actually found the problem. hThreadArray was not initialized.

Thanks though.
Topic archived. No new replies allowed.