Link to home
Start Free TrialLog in
Avatar of UdiRaz
UdiRaz

asked on

How does WaitForMultipleObjects works?

How does WaitForMultipleObjects works?
I guess it opens x threads and calls How does WaitForSingleObject works?
Can anyone give a more detailed explanation?code maybe

Thanks

Udi Raz
Avatar of jkr
jkr
Flag of Germany image

That is pretty much the way it works. Check out http://www.codeproject.com/KB/threads/WaitFunctions.aspx and the example code at http://www.codeproject.com/KB/threads/WaitFunctions/WaitFunctions_src.zip for more details. An example would be
void main()
{
    // Data of Thread 1
 
    int Data_Of_Thread_1 = 1;
    // Data of Thread 2
 
    int Data_Of_Thread_2 = 2;
    // Data of Thread 3
 
    int Data_Of_Thread_3 = 3;
    // variable to hold handle of Thread 1
 
    HANDLE Handle_Of_Thread_1 = 0;
    // variable to hold handle of Thread 1 
 
    HANDLE Handle_Of_Thread_2 = 0;
    // variable to hold handle of Thread 1
 
    HANDLE Handle_Of_Thread_3 = 0;
    // Aray to store thread handles 
 
    HANDLE Array_Of_Thread_Handles[3];
 
    // Create thread 1.
 
    Handle_Of_Thread_1 = CreateThread( NULL, 0, 
           Thread_no_1, &Data_Of_Thread_1, 0, NULL);  
    if ( Handle_Of_Thread_1 == NULL)
        ExitProcess(Data_Of_Thread_1);
    
    // Create thread 2.
 
    Handle_Of_Thread_2 = CreateThread( NULL, 0, 
           Thread_no_2, &Data_Of_Thread_2, 0, NULL);  
    if ( Handle_Of_Thread_2 == NULL)
        ExitProcess(Data_Of_Thread_2);
    
    // Create thread 3.
 
    Handle_Of_Thread_3 = CreateThread( NULL, 0, 
           Thread_no_3, &Data_Of_Thread_3, 0, NULL);  
    if ( Handle_Of_Thread_3 == NULL)
        ExitProcess(Data_Of_Thread_3);
 
 
    // Store Thread handles in Array of Thread
 
    // Handles as per the requirement
 
    // of WaitForMultipleObjects() 
 
    Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
    Array_Of_Thread_Handles[1] = Handle_Of_Thread_2;
    Array_Of_Thread_Handles[2] = Handle_Of_Thread_3;
    
    // Wait until all threads have terminated.
 
    WaitForMultipleObjects( 3, 
        Array_Of_Thread_Handles, TRUE, INFINITE);
 
    printf("Since All threads executed" 
           " lets close their handles \n");
 
    // Close all thread handles upon completion.
 
    CloseHandle(Handle_Of_Thread_1);
    CloseHandle(Handle_Of_Thread_2);
    CloseHandle(Handle_Of_Thread_3);
}
 
// taken from http://www.codeproject.com/KB/threads/Threads_1.aspx

Open in new window

When you say how do they work... do you mean what do they do or how do they do it? The MSDN is pretty clear on what they do so have you tried reading this? If you have what is it you don't understand?
if you have N threads, you should create an array of N of HANDLE structure. then create your N threads using your HANDLE array and then use WaitForMultipleObjects API.
I posted you a complete project that works.
regards.
#include <iostream.h>
#include <windows.h>
 
DWORD WINAPI thread1(LPVOID lparam)
{
	for(int i=0;i<100;i++)
		cout<<i<<endl;
	return 0;
}
 
DWORD WINAPI thread2(LPVOID lparam)
{
	for(int i=0;i<100;i++)
		cout<<"\t\t"<<i<<endl;
	return 0;
}
 
int main()
{
	int data1 = 1,data2 = 2,data3 = 3;
	HANDLE th1,th2;
	th1=CreateThread(NULL,0,thread1,&data1,0,NULL);
	if(th1==NULL)
		ExitProcess(data1);
	th2=CreateThread(NULL,0,thread2,&data2,0,NULL);
	if(th2==NULL)
		ExitProcess(data2);
	HANDLE hwndarray[2];
	hwndarray[0] = th1;
	hwndarray[1] = th2;
	WaitForMultipleObjects(2,hwndarray,TRUE,INFINITE);
	CloseHandle(th1);
	CloseHandle(th2);
	system("pause");
	return 0;
}

Open in new window

Avatar of UdiRaz
UdiRaz

ASKER

I'm sorry, I probably didn't make myself clear.
I am not asking how to use WaitForMultipleObjects.
I would like to know how does WaitForMultipleObjects is implemented?

Thanks,
Udi Raz
It waits till timeout (thirdparameter) become passed or all handles you passed via second parameter (the handles array) become closed. Don't forget it waits fo ALL handles to become closed, then it will exit and next line will be executed.
As stupid as it may sound - it is implemented as a call to 'KeWaitForMultipleObjects()' (http://msdn.microsoft.com/en-us/library/ms801633.aspx) where that is handled at kernel level: "The KeWaitForMultipleObjects routine puts the current thread into an alertable or nonalertable wait state until any or all of a number of dispatcher objects are set to a signaled state or (optionally) until the wait times out."
Avatar of UdiRaz

ASKER

jkr : so I guess my question now is how does KeWaitForMultipleObjects is implemented?

Below is what I think happens inside WaitForMultipleObjects ( and fix me if I am wrong )

I don't understand how does WaitForMultipleObjects ( or KeWaitForMultipleObjects ) creates n thread dynamicly and called n methods dynamicly

Am I made myself clear?

DWORD WINAPI WaitForMultipleObjects( DWORD nCount,const HANDLE lpHandles, BOOL bWaitAll, DWORD dwMilliseconds )
{
   for ( int i=0 ; i<nCount ; i++ )
   {
     Handle_Of_Thread_i = CreateThread( NULL, 0, 
           Thread_no_i, &Data_Of_Thread_i, 0, NULL);  
 
   }
}
 
void Thread_no_0(....)
{
  WaitForSingleObject(...)
}
 
void Thread_no_1(....)
{
   WaitForSingleObject(...)
 
}
 
void Thread_no_2(....)
{
    WaitForSingleObject(...)
 
}
.
.
.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of lhl60
lhl60

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry
" The event are probably owned by one if your other threads"
should be
"The handle is  probably owned by one if your other threads"