Link to home
Start Free TrialLog in
Avatar of Barry01
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.
Avatar of gelbert
gelbert

When you create thread, on that thread create semaphore(CreateSemaphore) and wait for it on main thread(WaitForSingleObject).

void Wait()
{
HANDLE hsema=OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE,"MySemaphore");
if(hsema!=NULL) //if semaphore exhists then wait
WaitForSingleObject( hsema, INFINITE);
}
Avatar of Barry01

ASKER

for eg  I have function

myfunction()
{
...
...
createthread(..)

Probfunction(outputofthread)

}

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
Avatar of Barry01

ASKER

I am using the CreateThread( ..) to create the thread.
Avatar of Barry01

ASKER

myfunction()
{
....
....
CreateThread(..)

//I want to wait here until
// the thread is done.

Probfunction(outputofthread)

}

Avatar of Barry01

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(hThread, INFINITE);

DWORD dwExitCode;
::GetExitCodeThread(hThread, &dwExitCode);

Probfunction(dwExitCode) ;

}
WaitForSingleObject(hThread...
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(hThread, &dwExitCode))
         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.

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,TRUE,FALSE,NULL);
createthread(..)
WaitForSingleObject(Event,INFINITE);
Probfunction(outputofthread)

}

   
To MDarling, you right :)
Avatar of Zoppo
BTW, MichaelS, the code you provided will use lot of processor time instead of the provided solutions from paulburns and gelbert...
Avatar of Barry01

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
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
To Zoppo.
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
100% agree
Avatar of Barry01

ASKER

there is something in the thread if I stop the function it stops.
Avatar of Barry01

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
Avatar of Barry01

ASKER

yes it is
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Barry01

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.
> 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