Boost logo

Boost Users :

Subject: Re: [Boost-users] [threads] How to test for running thread
From: Koen Vermeer (koen_at_[hidden])
Date: 2008-10-15 10:42:45

On Wed, 2008-10-15 at 14:49 +0100, Bill Somerville wrote:
> Koen Vermeer wrote:
> > Suppose I have a thread running, I issue an interrupt(), and the thread
> > starts to perform its cleanup code. As this may take some time, I'd like
> > to know when the thread actually is stopped. Is there some standard
> > approach on how to do this? I was thinking about testing if the thread
> > object equals Not-a-Thread, but I've yet to find out how...
> I think thread::join() or thread::timed_join() is what you are looking for.

Thanks for the suggestion. In my message, I regrettably didn't make it
very clear that I didn't want to simply wait for the thread to complete.
I want to probe the thread, so I know if it is running. If it still is
running, I'll do some other things and test again later on.

It seems that I cannot 'probe' the thread in this way. If I do
thread::join(), it doesn't return until the thread completes. If I do
thread::timed_join(), I'd need a very short period in order to get the
wanted probing behavior. However, thread::timed_join() only returns true
if the thread was running when calling thread::timed_join(), and
completed before the timeout period. With a very short timeout period,
that means that it always returns false, either because the thread was
running before the call, and still is running after the call, or because
the thread wasn't running at all.

I could, of course, set a flag at the end of my thread, or use the
at_thread_exit() function to do this. But I am not sure if there isn't
some easier way.

Or am I simply missing something here?


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at