0

I don't necessarily want to use join() and wait for a thread to finish, but more so I want to briefly check whether a thread is still executing or not. I did think of timed_join(0), but I'm not sure if that's safe at all. Any advice here?

AutoBotAM
  • 1,485
  • 3
  • 18
  • 24
  • possible duplicate of [How can I tell reliably if a boost thread has exited its run method?](http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method) – bobbymcr Nov 19 '11 at 04:37
  • The answer for that was to basically use `timed_join(0)` (or with a minimal timeout). If this is the only way to do this then how high should my timeout be to avoid race conditions? – AutoBotAM Nov 19 '11 at 04:46
  • 2
    It is impossible to avoid a race condition when you do this sort of check. If you do `timed_join(N)` the thread could finish *immediately* after that instruction, no matter what value of N you use. What are you actually trying to achieve? – bobbymcr Nov 19 '11 at 05:00
  • In my main thread I basically call `timed_join(0)` in a loop for all of the other threads, and use the return of that to (over all) handle program termination. Would the race condition be eliminated if `timed_join(0)` were called consistently like this? – AutoBotAM Nov 19 '11 at 05:16
  • 1
    No, you should be using an eventing/notification mechanism to have the threads signal when they are done and an overall event to wait for all of them to complete. This is known as a countdown latch (see http://msdn.microsoft.com/en-us/magazine/cc163427.aspx#S1 ). You could build one using a boost condition variable and an int counter protected by a boost mutex. – bobbymcr Nov 19 '11 at 05:47
  • 2
    Actually, an even simpler approach should be to just sequentially join on all the threads in a loop. There's no need to attempt to parallelize this since in the worst case, the first join will take longer than all other threads to complete, but by that point the rest of the joins will take zero time (because they are all done). – bobbymcr Nov 19 '11 at 05:48
  • Most of the threads that I'm monitoring are dependent upon each other (meaning if one terminates, the other must as well), so a simple join probably wouldn't suffice. However I could implement a condition variable for each thread, although I was hoping Boost had already implemented some sort of flag for this in their `thread` object. – AutoBotAM Nov 19 '11 at 06:26
  • "_so a simple join probably wouldn't suffice_" Why not? – curiousguy Nov 19 '11 at 07:15
  • Say that Thread1 has many dependencies on Thread2, and I call `Thread1.join(); Thread2.join();` to handle my program's termination. If Thread2 terminates before Thread1, then Thread1 will have major problems that will lead to a crash (or at least very strange program behavior). This is why I need to actively monitor thread activity consistently, somewhat like this: `while(true) {if(ThreadX.timed_join(0)) break;}`. Of course I may have to replace timed_join with a condition variable of some sort to avoid race conditions. – AutoBotAM Nov 19 '11 at 16:46
  • @bobbymcr, if you want you can make a generic answer covering the info you've shared here, I'll mark it off as the answer. Thanks for the great help. – AutoBotAM Nov 19 '11 at 16:59

1 Answers1

2

You should be using an eventing/notification mechanism to have the threads signal when they are done and an overall event to wait for all of them to complete. This is known as a countdown latch (see http://msdn.microsoft.com/en-us/magazine/cc163427.aspx#S1 ). You could build one using a boost condition variable and an int counter protected by a boost mutex.

An even simpler approach should be to just sequentially join on all the threads in a loop. There's no need to attempt to parallelize this since in the worst case, the first join will take longer than all other threads to complete, but by that point the rest of the joins will take zero time (because they are all done).

If you have hard dependencies on which threads are allowed to finish first and complex lifetime graphs, you should consider representing these with a class/data structure to raise the level of abstraction so that the outside waiter doesn't need to care directly about these details.

bobbymcr
  • 23,769
  • 3
  • 56
  • 67