Solved

Thread Problem

Posted on 2000-04-19
25
231 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sum13 challenge 24 90
matchUp  challenge 6 53
wordmultiple challenge 12 121
Message not shown 5 34
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now