Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to terminate a thread run by another thread



Permlink Replies: 2 - Last Post: Jun 13, 2017 9:41 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Asger Joergensen

Posts: 370
Registered: 11/18/08
How to terminate a thread run by another thread
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 13, 2017 3:13 AM
Hi

I have a program that starts a thread (thread_1) which then in turn starts
other threads one at a time, when thread_1 starts another thread (thread_2)
it waits for that thread to finish using thread_2->WaitFor().

These other threads can be slow, so thread_1 might be waiting for 50 sec.
or more.

As it is now I have a function Stop() in thread one that calls Terminate()
on thread_2, this seem to work, but I'm not sure if it always will.

My question is what is the right way to terminate these threads in case of
program closing.

Thanks in advance
Best regards
Asger
Arkady Semylio

Posts: 9
Registered: 5/25/17
Re: How to terminate a thread run by another thread
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 13, 2017 7:41 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:
Hi

I have a program that starts a thread (thread_1) which then in turn starts
other threads one at a time, when thread_1 starts another thread (thread_2)
it waits for that thread to finish using thread_2->WaitFor().

These other threads can be slow, so thread_1 might be waiting for 50 sec.
or more.

As it is now I have a function Stop() in thread one that calls Terminate()
on thread_2, this seem to work, but I'm not sure if it always will.

My question is what is the right way to terminate these threads in case of
program closing.

Remembering you have to escape from the thread's Execute function in a
cooperative way (gentle exit, i.e., you ask to the thread to terminate), depending
on the structure of your code, you have mainly two choiches (or a mix of these):

1) Usually you can check TThread::Terminated property in many points of the
thread_2 thread's Execute in order to escape as soon as possible in a secure
way.

2) If thread_2 is waiting at some point using :WaitForSingleObject
or ::WaitForMultipleObjects, you can choose to add an TEvent object
to the waiting list to kick the sleeping thread for an immediate exit.
You can "signal" (by SetEvent) the TEvent in the stop function just after
you have called the Terminate method. So thread_2 awakes from ::WaitFor???
API, makes the appropriate cleanup then exit gently from TThread::Execute
function.

Bye
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to terminate a thread run by another thread
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 13, 2017 9:41 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger Joergensen wrote:

I have a program that starts a thread (thread_1) which then in turn
starts other threads one at a time, when thread_1 starts another
thread (thread_2) it waits for that thread to finish using
thread_2->WaitFor().

Then what is the point of using secondary threads at all? Blocking one
thread while another thread runs to completion negates the benefits of
multi-threaded programing. You may as well just do all of thread_2's
work inside of thread_1 itself.

My question is what is the right way to terminate these threads in
case of program closing.

Assuming all of your threads are based on TThread, then the usual
Terminate()+WaitFor() combo applies, regardless of how many threads you
are running. Call Terminate() and WaitFor() on thread_1. In its
Execute(), it needs to check its Terminated property periodically and
exit as soon as possible. If it has already started a secondary
thread, it needs to call Terminate() and WaitFor() on that thread. And
so on.

Alternatively, you could just define a single global bool variable that
the main thread sets to true during shutdown, and then have all of your
threads check that variable in their Execute methods.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02