[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How does WaitForMultipleObjects works?

Posted on 2008-11-08
9
Medium Priority
?
2,472 Views
Last Modified: 2012-05-05
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
Comment
Question by:UdiRaz
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 22913817
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
 
LVL 40

Expert Comment

by:evilrix
ID: 22914385
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
 
LVL 17

Expert Comment

by:CSecurity
ID: 22915660
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:UdiRaz
ID: 22917165
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
 
LVL 17

Expert Comment

by:CSecurity
ID: 22917204
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
 
LVL 86

Expert Comment

by:jkr
ID: 22917234
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
 

Author Comment

by:UdiRaz
ID: 22919534
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
 
LVL 4

Accepted Solution

by:
lhl60 earned 2000 total points
ID: 22959083
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
 
LVL 4

Expert Comment

by:lhl60
ID: 22959096
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question