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
UdiRazAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
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

0
evilrixSenior Software Engineer (Avast)Commented:
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?
0
CSecurityCommented:
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

0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

UdiRazAuthor Commented:
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
0
CSecurityCommented:
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.
0
jkrCommented:
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."
0
UdiRazAuthor Commented:
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

0
lhl60Commented:
it does not create any threads. It puts YOUR thread to sleep until any or one of the handles you specified is signaled. The event are probably owned by one if your other threads
You must have obtained the handles from somewhere.

you can wait for any or all of these events handles

Change notification  
Console input  
Event  
Job  
Memory resource notification  
Mutex  
Process  
Semaphore  
Thread  
Waitable timer


When  a handle is signaled then your thread continue,

you can examine what handle generated the signal, by examining the return code
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhl60Commented:
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"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.