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.
Barry01Asked:
Who is Participating?
 
mikeblasConnect With a Mentor Commented:
> yes it is

Then, that's your problem. If a thread is stopped, it's not processing messages. If another thread sends it a message, it the sending thread will wait for the receiving thread to process the message--otherwise, it doesn't know the return code.

You need to work the wait call into your message pump. Using the Sleep() API as other people have suggested is easy, but it's sloppy and to be avoided because it wastes time--time that the working thread needs to do its job!

Instead of calling WaitForSingleObject(), you shoudl call MsgWaitForMultipleObjects(). That function will return if a message is put into your queue _or_ if your synchronous object (in your case, the other thread's handle) becomes signalled.

..B ekiM
0
 
gelbertCommented:
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);
}
0
 
Barry01Author Commented:
for eg  I have function

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

Probfunction(outputofthread)

}

so where should I create the semaphore
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gelbertCommented:
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
0
 
Barry01Author Commented:
I am using the CreateThread( ..) to create the thread.
0
 
Barry01Author Commented:
myfunction()
{
....
....
CreateThread(..)

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

Probfunction(outputofthread)

}

0
 
Barry01Author Commented:
if I put a wait in the function thread also stops
0
 
paulburnsCommented:
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) ;

}
0
 
MichaelSCommented:
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()
0
 
MDarlingCommented:
>> 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.

0
 
LIAOPCommented:
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)

}

   
0
 
MichaelSCommented:
To MDarling, you right :)
0
 
ZoppoCommented:
BTW, MichaelS, the code you provided will use lot of processor time instead of the provided solutions from paulburns and gelbert...
0
 
Barry01Author Commented:
with anykind of wait thread also stops.
0
 
mikeblasCommented:
> 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
0
 
mikeblasCommented:
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
0
 
MichaelSCommented:
To Zoppo.
Sure, I missed Sleep(1) in the loop.
0
 
mikeblasCommented:
> 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
0
 
MichaelSCommented:
100% agree
0
 
Barry01Author Commented:
there is something in the thread if I stop the function it stops.
0
 
Barry01Author Commented:
TO whom I have to give the points.
0
 
mikeblasCommented:
> 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
0
 
Barry01Author Commented:
yes it is
0
 
Barry01Author Commented:
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.
0
 
mikeblasCommented:
> 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

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.

All Courses

From novice to tech pro — start learning today.