[Boost-users] [thread] boost::thread automatically detached when thread terminates?

70 views
Skip to first unread message

Boris Schaeling

unread,
Sep 11, 2010, 5:00:56 PM9/11/10
to boost...@lists.boost.org
Is a boost::thread object automatically detached when a thread terminates?

I read in the Boost.Thread 1.44 documentation: "If the thread of execution
represented by the boost::thread object has already completed, or the
boost::thread object represents Not-a-Thread, then join() returns
immediately."

Is it really required to call join() or timed_join()? Or can I also call
joinable() which would return false then?

Boris

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Lars Viklund

unread,
Sep 11, 2010, 5:22:12 PM9/11/10
to boost...@lists.boost.org
On Sat, Sep 11, 2010 at 11:00:56PM +0200, Boris Schaeling wrote:
> Is a boost::thread object automatically detached when a thread terminates?
>
> I read in the Boost.Thread 1.44 documentation: "If the thread of
> execution represented by the boost::thread object has already completed,
> or the boost::thread object represents Not-a-Thread, then join() returns
> immediately."
>
> Is it really required to call join() or timed_join()? Or can I also call
> joinable() which would return false then?

The four ways of cleaning up properly after a thread with Boost.Thread
are:

1) detach() to make the boost::thread object not refer to a thread of
execution anymore and any associated thread resources properly released;

2) have the boost::thread object die a natural death (upon which it will
detach() in the destructor, resulting in 1);

3) join(), after return of the boost::thread will not refer to a thread
of execution and the thread will have been cleaned up;

4) a successful timed_join(), with the same results as 3).

Note that in no case it's sane to do things like { new thread(&f); },
as you're not only leaking a thread object, but also OS thread
primitives, resulting in zombies and resource exhaustion.

I hope this clears some things up.

--
Lars Viklund | z...@acc.umu.se

Boris Schaeling

unread,
Sep 11, 2010, 6:04:08 PM9/11/10
to boost...@lists.boost.org
On Sat, 11 Sep 2010 23:22:12 +0200, Lars Viklund <z...@acc.umu.se> wrote:

> On Sat, Sep 11, 2010 at 11:00:56PM +0200, Boris Schaeling wrote:
>> Is a boost::thread object automatically detached when a thread
>> terminates?
>>
>> I read in the Boost.Thread 1.44 documentation: "If the thread of
>> execution represented by the boost::thread object has already completed,
>> or the boost::thread object represents Not-a-Thread, then join() returns
>> immediately."
>>
>> Is it really required to call join() or timed_join()? Or can I also call
>> joinable() which would return false then?
>
> The four ways of cleaning up properly after a thread with Boost.Thread
> are:

Sorry, I wasn't clear then. I'm trying to find out how I can easily tell
whether a thread of execution has terminated:

boost::thread t(...);
// thread of execution runs
bool running = t.joinable();
// thread of execution terminates (automatically)
bool still_running = t.joinable();

Does joinable() return true in the first case and false in the second? The
documentation says that in the second case join() returns immediately.
However I don't want to call join() because it would block in the first
case. I would need to know in advance whether the thread of execution has
terminated or not - this is exactly what I'd like boost::thread to tell
me. :)

Boris

Francesco Biscani

unread,
Sep 12, 2010, 12:52:49 AM9/12/10
to boost...@lists.boost.org
Hi Boris,

On Sun, Sep 12, 2010 at 12:04 AM, Boris Schaeling <bo...@highscore.de> wrote:
> Sorry, I wasn't clear then. I'm trying to find out how I can easily tell
> whether a thread of execution has terminated:
>
> boost::thread t(...);
> // thread of execution runs
> bool running = t.joinable();
> // thread of execution terminates (automatically)
> bool still_running = t.joinable();
>
> Does joinable() return true in the first case and false in the second?

No, it will return true both times.

> The
> documentation says that in the second case join() returns immediately.
> However I don't want to call join() because it would block in the first
> case. I would need to know in advance whether the thread of execution has
> terminated or not - this is exactly what I'd like boost::thread to tell me.
> :)

Maybe you can use boost::thread::timed_join() with a very low timeout
(or even zero timeout, haven't checked if it works like that).

> Boris

Cheers,

Francesco.

Boris Schaeling

unread,
Sep 12, 2010, 8:39:49 AM9/12/10
to boost...@lists.boost.org
On Sun, 12 Sep 2010 06:52:49 +0200, Francesco Biscani
<blues...@gmail.com> wrote:

> [...]


>> boost::thread t(...);
>> // thread of execution runs
>> bool running = t.joinable();
>> // thread of execution terminates (automatically)
>> bool still_running = t.joinable();
>>
>> Does joinable() return true in the first case and false in the second?
>
> No, it will return true both times.
>
>> The
>> documentation says that in the second case join() returns immediately.
>> However I don't want to call join() because it would block in the first
>> case. I would need to know in advance whether the thread of execution
>> has
>> terminated or not - this is exactly what I'd like boost::thread to tell
>> me.
>> :)
>
> Maybe you can use boost::thread::timed_join() with a very low timeout
> (or even zero timeout, haven't checked if it works like that).

Thanks for your reply! Do you know whether the thread of execution is
allowed to call boost::thread::detach()? Then it could detach itself from
the boost::thread object before it terminates and
boost::thread::joinable() should work? I'm not sure though if
boost::thread is thread-safe?

Boris

Eric J. Holtman

unread,
Sep 12, 2010, 8:44:42 AM9/12/10
to boost...@lists.boost.org
On 9/12/2010 7:39 AM, Boris Schaeling wrote:
>
> Thanks for your reply! Do you know whether the thread of execution is
> allowed to call boost::thread::detach()? Then it could detach itself
> from the boost::thread object before it terminates and
> boost::thread::joinable() should work? I'm not sure though if
> boost::thread is thread-safe?
>

What are you trying to accomplish?

If you don't care when a thread terminates, call detach right
after you create it:

main thread:
boost::thread t (...);
t.detach ();

Boris Schaeling

unread,
Sep 12, 2010, 8:55:30 AM9/12/10
to boost...@lists.boost.org
On Sun, 12 Sep 2010 14:44:42 +0200, Eric J. Holtman <er...@holtmans.com>
wrote:

> On 9/12/2010 7:39 AM, Boris Schaeling wrote:
>>
>> Thanks for your reply! Do you know whether the thread of execution is
>> allowed to call boost::thread::detach()? Then it could detach itself
>> from the boost::thread object before it terminates and
>> boost::thread::joinable() should work? I'm not sure though if
>> boost::thread is thread-safe?
>>
>
> What are you trying to accomplish?

I want a worker thread to terminate if there is nothing to do. If there is
something to do again and the worker thread has terminated I need to
create a new one. Now I wonder if I can use boost::thread to detect
whether the thread is running or not.

Boris

Ray Burkholder

unread,
Sep 12, 2010, 9:12:35 AM9/12/10
to boost...@lists.boost.org
> >> Thanks for your reply! Do you know whether the thread of execution is
> >> allowed to call boost::thread::detach()? Then it could detach itself
> >> from the boost::thread object before it terminates and
> >> boost::thread::joinable() should work? I'm not sure though if
> >> boost::thread is thread-safe?
>
> I want a worker thread to terminate if there is nothing to do. If there
> is
> something to do again and the worker thread has terminated I need to
> create a new one. Now I wonder if I can use boost::thread to detect
> whether the thread is running or not.
>

How about using boost::asio. It automatically deals with worker threads and
such.

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Eric J. Holtman

unread,
Sep 12, 2010, 9:32:13 AM9/12/10
to boost...@lists.boost.org
On 9/12/2010 7:55 AM, Boris Schaeling wrote:
>
> I want a worker thread to terminate if there is nothing to do. If
> there is something to do again and the worker thread has terminated I
> need to create a new one. Now I wonder if I can use boost::thread to
> detect whether the thread is running or not.
>

The reason boost::thread doesn't provide you with an
"is_running" function, is that it would be nearly impossible
to write it correctly.

Example:

main thread starts worker.
worker starts work.
main thread calls is_running which returns true.

now, before main thread can execute next statement, worker thread exits.

main thread tries to send more work to worker, who is gone.

Roland Bock

unread,
Sep 12, 2010, 9:33:06 AM9/12/10
to boost...@lists.boost.org
On 09/12/2010 02:55 PM, Boris Schaeling wrote:
> I want a worker thread to terminate if there is nothing to do. If
> there is something to do again and the worker thread has terminated I
> need to create a new one. Now I wonder if I can use boost::thread to
> detect whether the thread is running or not.
>
> Boris
>
You might also want to think about a thread pool: A number of worker
threads which do something like this:

while(true)
{
wait_for_task;
process_task;
}

Rather simple to implement and more effective (I think) than starting a
new thread for each task. It also allows you to control the number of
total worker threads easily. It is also simple to keep track of the
number of working/waiting worker threads.


Regards,

Roland

Boris Schaeling

unread,
Sep 12, 2010, 12:05:30 PM9/12/10
to boost...@lists.boost.org
On Sun, 12 Sep 2010 15:12:35 +0200, Ray Burkholder <r...@oneunified.net>
wrote:

>> >> Thanks for your reply! Do you know whether the thread of execution is
>> >> allowed to call boost::thread::detach()? Then it could detach itself
>> >> from the boost::thread object before it terminates and
>> >> boost::thread::joinable() should work? I'm not sure though if
>> >> boost::thread is thread-safe?
>>
>> I want a worker thread to terminate if there is nothing to do. If there
>> is
>> something to do again and the worker thread has terminated I need to
>> create a new one. Now I wonder if I can use boost::thread to detect
>> whether the thread is running or not.
>>
>
> How about using boost::asio. It automatically deals with worker threads
> and
> such.

I need the worker thread within a Boost.Asio extension. ;)

Boris

Boris Schaeling

unread,
Sep 12, 2010, 12:11:54 PM9/12/10
to boost...@lists.boost.org
On Sun, 12 Sep 2010 15:32:13 +0200, Eric J. Holtman <er...@holtmans.com>
wrote:

> On 9/12/2010 7:55 AM, Boris Schaeling wrote:


>>
>> I want a worker thread to terminate if there is nothing to do. If there
>> is something to do again and the worker thread has terminated I need to
>> create a new one. Now I wonder if I can use boost::thread to detect
>> whether the thread is running or not.
>>
>
> The reason boost::thread doesn't provide you with an
> "is_running" function, is that it would be nearly impossible
> to write it correctly.
>
> Example:
>
> main thread starts worker.
> worker starts work.
> main thread calls is_running which returns true.
>
> now, before main thread can execute next statement, worker thread exits.
>
> main thread tries to send more work to worker, who is gone.

Good point (and so obvious :)!

Thanks,
Boris

Ray Burkholder

unread,
Sep 12, 2010, 3:22:23 PM9/12/10
to boost...@lists.boost.org
> >
> > How about using boost::asio. It automatically deals with worker
> threads
> > and
> > such.
>
> I need the worker thread within a Boost.Asio extension. ;)
>

You can create multiple asio services, with each service having its own
worker thread(s). So the extension, would have its own service, which deals
with the worker thread(s) for you. Possibly over kill, but is the easy way
out from reinventing the wheel. Or I may just not understand the problem.


Ray


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

_______________________________________________

Reply all
Reply to author
Forward
0 new messages