Boost logo

Boost Users :

Subject: Re: [Boost-users] [threads] How to test for running thread
From: Bill Somerville (bill_at_[hidden])
Date: 2008-10-15 11:16:47

Koen Vermeer wrote:
> 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?
I understand, I did wonder if you wanted an asynchronous notification.

A thread can be in a zombie state where it has completed but has not
been joined, thread::timed_join() will return true when called on a
zombie thread even if the thread was not running before the call.

I would go with the timed_join() with a short timeout. Threads wait
around for a join holding their final exit status so there is no race
condition that I am aware of, thread termination and joining is
> Best,
> Koen
Bill Somerville

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