How do I kill all the running threads in RWThreadPool?

Article ID: 1183
Last updated: 07 Feb, 2008
Article ID: 1183
Last updated: 07 Feb, 2008
Revision: 1
Views: 3040
Posted: 01 Jan, 2000
by Dean J.
Updated: 07 Feb, 2008
by Dean J.
Problem


How do I kill all the running threads in RWThreadPool?





Action


You can not kill the running threads in an RWThreadPool. It is not considered a good practice to kill running threads, as they may be holding system resources like file handles, database connections, or network resources, or the threads may have acquired synchronization object like a mutex. To get around these problems, you can design your application to make your threads exit safely under abnormal conditions.

You can write a cancellation mechanism that will stop the threads that enter blocking state or infinite loops. For the RWThreadPool, you need to write your own cancellation mechanism (which is fairly simple) because the threads just execute the Functor passed in.

<pre>
RWBoolean Die = FALSE; // Tell running threads to stop...


RWBoolean CanThreadRun(void) {
    return Die;
}
void StopThreads(void) { // threads are told to stop
    Die = TRUE;
}
void AllowThreads(void) { // and are allowed to go
    Die = FALSE;
}
void WorkFunction(RWFunctor0<RWBoolean> func) {
    while(!func())
    {
       ; // loop
    }
}

int main(void)
{
 RWThreadPool pool = RWThreadPool::make();
 pool.start();
 AllowThreads(); // allow threads to run
 RWTFunctorR0G<RWBoolean> check= RWTFunctorR0GImp<RWBoolean>::make  (CanThreadRun);
 // enqueue some work
 RWFunctor0 work = RWFunctor0GA1Imp<void, RWTFunctorR0G<RWBoolean> >::make(WorkFunction, check);
 pool.enqueue(work);
 
 // need to stop thread running work...
 StopThreads(); // tell threads to stop
 // more work or whatever...
 AllowThreads(); // allow threads to run
 // enqueue the work
 StopThreads(); // tell threads to stop
 return 0;
}
</pre>

If you need finer granularity, an RWBoolean for each thread will be sufficient. You don't need to use a functor, but it makes it much easier to abstract away the details of stopping a particular thread. Another thing you could do would be to use IOUs or a simple Boolean flag that tells a particular Thread (or Thread group) to quit. Refer to knowledge base article:  How do I stop a thread that is blocked on a system call or performing I/O? on how to use the IOUs to terminate threads using a cancellation mechanism.

 

This article was:   Helpful | Not helpful
Report an issue
Article ID: 1183
Last updated: 07 Feb, 2008
Revision: 1
Views: 3040
Posted: 01 Jan, 2000 by Dean J.
Updated: 07 Feb, 2008 by Dean J.
Also read

Also listed in


Others in this category