Re: [Boost-bugs] [Boost C++ Libraries] #4345: thread::id and joining problem with cascade of threads

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #4345: thread::id and joining problem with cascade of threads
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-06-16 05:51:22


#4345: thread::id and joining problem with cascade of threads
----------------------------------------------------------+-----------------
 Reporter: bartek szurgot <bartosz.szurgot@…> | Owner: anthonyw
     Type: Bugs | Status: new
Milestone: Boost 1.43.0 | Component: thread
  Version: Boost 1.43.0 | Severity: Problem
 Keywords: |
----------------------------------------------------------+-----------------

Comment(by bartek szurgot <bartosz.szurgot@…>):

 Replying to [comment:2 steven_watanabe]:
> This code is very hard to follow, but here's what's happening in detail:

 this is the easy case - trust me. i've spent last 2 days tracking it down
 in "real" system we're developing. ;)

> - An object of type B is created
> - This creates a shared_ptr with a new A object
> - The new A object starts thread A
> - Thread B is created, and receives a copy of the B object, this copy is
 not
> destroyed until the last thread::id for this thread is gone.

 this is the key - thread::id holds identifier of a thread as well as
 handle to it. it means that thread::id has in fact double responsibility,
 thus non-obvious behavior (id!=handle).

> - Thread B runs to completion and is joined. The last copy of B has not
 yet
> been destroyed, so there is no attempt to join thread A.
> - Kaboom.
>
> I don't think this should be considered a bug in the library. If you
 want to join another thread when a thread exits, join at the end of the
 thread function instead of in the destructor.

 technically this can be done, though i don't think it's a proper way - by
 doing so one must remember in each thread's operator()()'s implementation
 to explicitly join all owned threads, instead of doing it in error-prone,
 RAII-way (d-tor perform clean up - here: int+join).

 a took a look in thread::id implementation - it holds shared_ptr to
 "thread's data" (i assume this is a handle to thread itself). my
 suggestion is to change it to weak_ptr, so that thread::id would become
 only "observer" of the thread, that can be compared, put into stream,
 etc... this would eliminate double responsibility of thread::id.
 alternatively only data required to identify thread should be stored in
 thread::id - ex.: name string.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/4345#comment:4>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:03 UTC