Changes between Version 2 and Version 3 of Ticket #7541


Ignore:
Timestamp:
Oct 23, 2012, 8:06:28 PM (10 years ago)
Author:
viboes
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #7541 – Description

    v2 v3  
    1 Based on the scoped_thread class defined in C++ Concurrency in Action define a thread wrapper class that instead of calling terminate if the thread is joinable on destruction, interrupts and join it if joinable.
     1Based on the scoped_thread class defined in C++ Concurrency in Action define a thread wrapper class that instead of calling terminate if the thread is joinable on destruction, join it if joinable.
    22
    33While the scoped_thread class defined in C++ Concurrency in Action is a strict scoped class that doesn't allows any change in the wrapped thread, I think that making the interface thread compatible is also a good option. This doesn't means that a strict scoped thread has no use.
     
    66namespace boost
    77{
    8   class scoped_thread
     8  class scoped_thread : public thread
    99  {
    10     thread t;
     10    typedef thread base_type;
    1111  public:
    12     BOOST_THREAD_NO_COPYABLE( scoped_thread)
     12    BOOST_THREAD_MOVABLE_ONLY( scoped_thread )
     13
    1314    explicit scoped_thread(BOOST_THREAD_RV_REF(thread) t_) :
    14       t(move(t_))
     15      base_type(boost::forward<base_type>(t_)),
     16      m_(*static_cast<base_type*>(this))
    1517    {
    1618    }
    17     inline thread::id get_id() const BOOST_NOEXCEPT
     19
     20    scoped_thread(BOOST_RV_REF(scoped_thread) x) :
     21      base_type(boost::move(static_cast<base_type&>(x)))
     22    { }
     23
     24    scoped_thread& operator=(BOOST_RV_REF(scoped_thread) x)
    1825    {
    19       return t.get_id();
    20     }
    21     void swap(scoped_thread& x)BOOST_NOEXCEPT
    22     {
    23       t.swap(x.t);
    24     }
    25     void detach()
    26     {
    27       t.detach();
    28     }
    29     void join()
    30     {
    31       t.join();
    32     }
    33     template <class Rep, class Period>
    34     bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
    35     {
    36       return t.try_join_for(rel_time);
    37     }
    38     template <class Clock, class Duration>
    39     bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time)
    40     {
    41       return t.try_join_until(abs_time);
    42     }
    43     thread::native_handle_type native_handle() BOOST_NOEXCEPT {
    44       return t.native_handle();
    45     }
    46     void interrupt()
    47     {
    48       t.interrupt();
     26      base_type::operator=(boost::move(static_cast<base_type&>(x)));
     27      return *this;
    4928    }
    5029
    51     bool interruption_requested() const BOOST_NOEXCEPT
    52     {
    53       return t.interruption_requested();
    54     }
    5530    ~scoped_thread()
    5631    {
    57       t.interrupt();
    58       if (t.joinable())
     32      if (joinable())
    5933      {
    60         t.join();
     34        join();
    6135      }
    6236    }