Rogue Wave Knowledge Base
Search:    
Browse by category:
Knowledgebase | Glossary | Ask a Question |

How do I kill all the running threads in RWThreadPool?

Article ID: 1183
Last updated: 07 Feb, 2008
Revision: 1
print  Print
share  Share
Views: 3028
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


Also read
item How do I stop a thread that is blocked on a system call or performing I/O?

Also listed in
folder Legacy -> .h++

Prev     Next
Explain the terms unsafe, MT Safe 1, MT Safe 2 and MT-hot.       Compiler error: 'An Unknown Threading API has been specified..'

Others in this category
b What is the maximum number of Threads that can be created on a system.
b Significant slowdown of my multithreaded application when it uses Threads module.
b How do I kill all the running threads with in RWServerPool?
b Difference between RWServerPool and RWThreadPool
b Explain the terms unsafe, MT Safe 1, MT Safe 2 and MT-hot.
b Compiler error: 'An Unknown Threading API has been specified..'
b Migration Guide Tools.h++ 7.x -> SourcePro Core Essential Tools Module
b Using RWThreadPool from the SourcePro Threads Module with SourcePro DB