views:

205

answers:

3

I assumed joinable would indicate this, however, it does not seem to be the case.

In a worker class, I was trying to indicate that it was still processing through a predicate:

bool isRunning(){return thread_->joinable();}

Wouldn't a thread that has exited not be joinable? What am I missing... what is the meaning of boost thread::joinable?

+1  A: 

You fundamentally can't do this. The reason is that the two possible answers are "Yes" and "Not when I last looked but perhaps now". There is no reliable way to determine that a thread is still inside its run method, even if there was a reliable way to determine the opposite.

MSalters
You fundamentally _can_ do this. You can reliably query or "observe" the status of a thread at any given time. The problem is just that you cannot make any assumptions for interactions based upon the status you got back, meaning the moment you have the info "the thread is still running" it could already have been stopped in the time it took to get that information, but that is rarely critical. Sorry but this post was not in any way helpful.
AndreasT
+1  A: 

Use thread::timed_join() with a minimal timeout. It will return false if the thread is still running.

Joakim Karlsson
+4  A: 

Since you can join a thread even after it has terminated, joinable() will still return true until you call join() or detach(). If you want to know if a thread is still running, you should be able to call timed_join with a wait time of 0. Note that this can result in a race condition since the thread may terminate right after the call.

interjay
It's a bit misleading to say that it can result in a race condition. `timed_join` alone can't do that. if you make any incorrect assumptions based on the result of the call, you can end up with a race condition, of course, but that's less to do with `timed_join` than with you assuming that the result of that call is still valid. Anyway, +1
jalf
I think this is really bad. Why do the boost designers never think of intuitivity or of beginners. A simple isRunning() method would have been enough. Instead they force one to use a function that should intuitively not be used in this case. I don't want to try and join the thread, I just want to quickly test if it still does something. This is crap. Wasted a lot of time before I found this. Not a damn word in the "docs" either.
AndreasT