Solved

Thread Problem

Posted on 2000-04-19
25
236 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:Barry01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 4
  • +5
25 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 2731920
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
 

Author Comment

by:Barry01
ID: 2732222
for eg  I have function

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

Probfunction(outputofthread)

}

so where should I create the semaphore
0
 
LVL 8

Expert Comment

by:gelbert
ID: 2732295
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
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:Barry01
ID: 2732351
I am using the CreateThread( ..) to create the thread.
0
 

Author Comment

by:Barry01
ID: 2732420
myfunction()
{
....
....
CreateThread(..)

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

Probfunction(outputofthread)

}

0
 

Author Comment

by:Barry01
ID: 2732746
if I put a wait in the function thread also stops
0
 
LVL 2

Expert Comment

by:paulburns
ID: 2732885
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
 
LVL 6

Expert Comment

by:MichaelS
ID: 2733585
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
 
LVL 3

Expert Comment

by:MDarling
ID: 2733720
>> 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
 

Expert Comment

by:LIAOP
ID: 2733740
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
 
LVL 6

Expert Comment

by:MichaelS
ID: 2733751
To MDarling, you right :)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2733947
BTW, MichaelS, the code you provided will use lot of processor time instead of the provided solutions from paulburns and gelbert...
0
 

Author Comment

by:Barry01
ID: 2735259
with anykind of wait thread also stops.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2736982
> 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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2736993
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
 
LVL 6

Expert Comment

by:MichaelS
ID: 2737105
To Zoppo.
Sure, I missed Sleep(1) in the loop.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2737796
> 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
 
LVL 6

Expert Comment

by:MichaelS
ID: 2743144
100% agree
0
 

Author Comment

by:Barry01
ID: 2744029
there is something in the thread if I stop the function it stops.
0
 

Author Comment

by:Barry01
ID: 2744106
TO whom I have to give the points.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2746343
> 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
 

Author Comment

by:Barry01
ID: 2748197
yes it is
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 200 total points
ID: 2749932
> 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
 

Author Comment

by:Barry01
ID: 2751905
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2753460
> 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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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