venkysmarty
venkysmarty

Reputation: 11441

Thread deletion design

I have multi thread program. I have a design of my application as follows:

Suppose one is main thread, and other are slave threads. Main thread keep track of all slave thread ID's. During one of the scenario of application (one of the scenario is graceful shutdown of application), i want to delete slave threads from main thread. Here slave threads may be executing i.e., either in sleep mode or doing some action which i cannot stop the action. So i want to delete the threads from main thread with thread IDs i stored internally.

Additional info: While deleting i should not wait for thread current action to complete as it may take long time as i am reading from data base and taking some action in thread, in case of gracefull shut down i should not wait for action to complete as it may take time.

If i force delete a thread how can there will be a resource leaks?

Is above design is ok or there is any flow or any ways we can improve the design.

Thanks!

Upvotes: 0

Views: 94

Answers (2)

Miguel Grinberg
Miguel Grinberg

Reputation: 67492

The most common approach consists in the main thread sending a termination signal to all the threads, then waiting for the threads to end.

Typically the worker threads will have a loop, inside of which the work is done. You can add a boolean variable that indicates if the thread needs to end. For example:

terminate = false;
while (!terminate) {
    // work here
}

If you want your worker threads to go to sleep when they have no work, then it gets a bit more complicated. In this case you could make the threads wait on semaphores. Each semaphore will be signaled when there is work to do, and that will awaken the thread. You will also signal the semaphore when the request to terminate is issued. Example worker thread:

terminate = false;
while (!terminate) {
    // work here
    wait(semaphore); // go to sleep
}

When the main thread wants to exit it will set terminate to true for all the threads and then signal the thread semaphores to awaken the threads and give them a chance to see the termination request. After that it will join all the threads, and only after all the threads are finished it will exit.

Note that the terminate boolean may need to be declared as volatile if you are using C/C++, to indicate to the compiler that it may be changed from another thread.

Upvotes: 0

JosephH
JosephH

Reputation: 8825

It's not okay. It's a bad practice to forcefully kill a thread from another thread because you'll very likely to have resource leaks. The best way is to use an event or signal to signal the client process to stop and wait until they exit gracefully.

The overall flow of the program would look like this:

  1. Parent thread creates an event (say hEventParent). it then creates child threads and passes hEventParent as a parameter. The Parent thread keeps the hThread of the child thread(s).
  2. Child threads do work but periodically waits for hEventParent.
  3. When the program needs to exit, the parent thread sets hEventParent. It then waits for hThread (WaitForMultipleObjects also accepts hThread)
  4. Child thread is notified then execute clean up routine and exits.
  5. When all the threads exit, the parent can then exit.

Upvotes: 1

Related Questions