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
I guess it opens x threads and calls How does WaitForSingleObject works?
Can anyone give a more detailed explanation?code maybe
Thanks
Udi Raz
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.
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;
}
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
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."
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?
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(...)
}
.
.
.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
" The event are probably owned by one if your other threads"
should be
"The handle is probably owned by one if your other threads"
Open in new window