• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2576
  • Last Modified:

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
0
UdiRaz
Asked:
UdiRaz
  • 2
  • 2
  • 2
  • +2
1 Solution
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now