Barry01
asked on
Thread Problem
I am creating a thread in a function in visual C++ and Now I want to wait until the thread completes
then some other function to the output created by that thread but if I stops the function
the thread also stops.
then some other function to the output created by that thread but if I stops the function
the thread also stops.
ASKER
for eg I have function
myfunction()
{
...
...
createthread(..)
Probfunction(outputofthrea d)
}
so where should I create the semaphore
myfunction()
{
...
...
createthread(..)
Probfunction(outputofthrea
}
so where should I create the semaphore
You create semaphore inside your thread class. If you are using CWinThread derived class then do it in InitInstance() and release it in ExitInstance(), if you are using AfxBeginThread() then do it in the beginnning and at the end of your function
ASKER
I am using the CreateThread( ..) to create the thread.
ASKER
myfunction()
{
....
....
CreateThread(..)
//I want to wait here until
// the thread is done.
Probfunction(outputofthrea d)
}
{
....
....
CreateThread(..)
//I want to wait here until
// the thread is done.
Probfunction(outputofthrea
}
ASKER
if I put a wait in the function thread also stops
Wait on the thread handle...
myfunction()
{
.....
.....
HANDLE hThread = CreateThread(..)
//I want to wait here until
// the thread is done.
::WaitForSingleObject(hThr ead, INFINITE);
DWORD dwExitCode;
::GetExitCodeThread(hThrea d, &dwExitCode);
Probfunction(dwExitCode) ;
}
myfunction()
{
.....
.....
HANDLE hThread = CreateThread(..)
//I want to wait here until
// the thread is done.
::WaitForSingleObject(hThr
DWORD dwExitCode;
::GetExitCodeThread(hThrea
Probfunction(dwExitCode) ;
}
WaitForSingleObject(hThrea d...
will not works
You have a few ways to complete that task and as for me the easyest is:
HANDLE hThread = CreateThread(...);
DWORD dwExitCode;
do
{ if(!GetExitThreadCode(hThr ead, &dwExitCode))
break;
}while(dwExitCode == STILL_ACTIVE);
or you can create Events, Semaphores or whatever and use something like WaitForSingleObject()
will not works
You have a few ways to complete that task and as for me the easyest is:
HANDLE hThread = CreateThread(...);
DWORD dwExitCode;
do
{ if(!GetExitThreadCode(hThr
break;
}while(dwExitCode == STILL_ACTIVE);
or you can create Events, Semaphores or whatever and use something like WaitForSingleObject()
>> WaitForSingleObject won't work
Why not? I use that method all the time. As far as I'm aware that's the correct method. Paul's solution is correct.
Why not? I use that method all the time. As far as I'm aware that's the correct method. Paul's solution is correct.
You can create a event as agoble variable firstly, then when the thread function finish, set the event. After create the thread, use waitforsingleobject to wait the result.
HANDLE Event;
createthread()
{
....
....
SetEvent(Event);
}
myfunction()
{
....
....
Event=CreateEvent(NULL,TRU E,FALSE,NU LL);
createthread(..)
WaitForSingleObject(Event, INFINITE);
Probfunction(outputofthrea d)
}
HANDLE Event;
createthread()
{
....
....
SetEvent(Event);
}
myfunction()
{
....
....
Event=CreateEvent(NULL,TRU
createthread(..)
WaitForSingleObject(Event,
Probfunction(outputofthrea
}
To MDarling, you right :)
BTW, MichaelS, the code you provided will use lot of processor time instead of the provided solutions from paulburns and gelbert...
ASKER
with anykind of wait thread also stops.
> with anykind of wait thread also stops.
The waiting thread stops. The thread being waited on does not stop. What good is code that sits and loops, calling GetExitCodeThread()? It's not stopped, but it's wasting CPU time. It'll take longer for the thread being waited on to stop because the thread you're waiting for gets less CPU time.
..B ekiM
The waiting thread stops. The thread being waited on does not stop. What good is code that sits and loops, calling GetExitCodeThread()? It's not stopped, but it's wasting CPU time. It'll take longer for the thread being waited on to stop because the thread you're waiting for gets less CPU time.
..B ekiM
Do you not want the waiting thread to stop executing until the subordinate thread finishes? Why not? Are you worried about it processing messages, or something? If so, just specify a zero timeout and keep pumping messages.
Note that your waiting thread will stop when it calls GetMessage() in your message pump and there are no waiting messages, too.
..B ekiM
Note that your waiting thread will stop when it calls GetMessage() in your message pump and there are no waiting messages, too.
..B ekiM
To Zoppo.
Sure, I missed Sleep(1) in the loop.
Sure, I missed Sleep(1) in the loop.
> Sure, I missed Sleep(1) in the loop.
Even with the addition of Sleep(1), the loop is substantially less efficient than waiting for the object.
..B ekiM
Even with the addition of Sleep(1), the loop is substantially less efficient than waiting for the object.
..B ekiM
100% agree
ASKER
there is something in the thread if I stop the function it stops.
ASKER
TO whom I have to give the points.
> there is something in the thread if I stop the function it stops.
You can find out yourself if you use the debugger, but here's my guess: Is one thread sending messages to the other? If so, that's it.
..B ekiM
You can find out yourself if you use the debugger, but here's my guess: Is one thread sending messages to the other? If so, that's it.
..B ekiM
ASKER
yes it is
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When we start a thread in function does it have anything to do whether that function is terminated or still running.
what if that function is in the infinite loop.
what if that function is in the infinite loop.
> When we start a thread in function does it have anything
> to do whether that function is terminated or still running.
I can't understand your question. If the function is terminated, it can't make any calls.
If the function is in an infinite loop, the other thread will still start. But if the other thread needs to send messages to the thread stuck in the loop, or the thread stuck in the loop owns a resource the new thread is trying to aquire, then nothing will happen in the new thread--you're causing a deadlock. It's blocking on a resource it can't hope to get.
Remember that a thread must run through its message loop if it expects to process messages, and that not handling messages can cause other threads in your app to hang as they wait for their messages to be processed. You've told me that's what is happening in your application.
..B ekiM
> to do whether that function is terminated or still running.
I can't understand your question. If the function is terminated, it can't make any calls.
If the function is in an infinite loop, the other thread will still start. But if the other thread needs to send messages to the thread stuck in the loop, or the thread stuck in the loop owns a resource the new thread is trying to aquire, then nothing will happen in the new thread--you're causing a deadlock. It's blocking on a resource it can't hope to get.
Remember that a thread must run through its message loop if it expects to process messages, and that not handling messages can cause other threads in your app to hang as they wait for their messages to be processed. You've told me that's what is happening in your application.
..B ekiM
void Wait()
{
HANDLE hsema=OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE,"MySemaphore");
if(hsema!=NULL) //if semaphore exhists then wait
WaitForSingleObject( hsema, INFINITE);
}