Re: [Boost-bugs] [Boost C++ Libraries] #7541: Add a thread wrapper class that interrupts and join on destruction

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #7541: Add a thread wrapper class that interrupts and join on destruction
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-10-23 20:06:28


#7541: Add a thread wrapper class that interrupts and join on destruction
-------------------------------+--------------------------------------------
  Reporter: viboes | Owner: viboes
      Type: Feature Requests | Status: assigned
 Milestone: To Be Determined | Component: thread
   Version: Boost 1.52.0 | Severity: Cosmetic
Resolution: | Keywords:
-------------------------------+--------------------------------------------
Description changed by viboes:

Old description:

> 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.
>
> While 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.
>
> {{{
> namespace boost
> {
> class scoped_thread
> {
> thread t;
> public:
> BOOST_THREAD_NO_COPYABLE( scoped_thread)
> explicit scoped_thread(BOOST_THREAD_RV_REF(thread) t_) :
> t(move(t_))
> {
> }
> inline thread::id get_id() const BOOST_NOEXCEPT
> {
> return t.get_id();
> }
> void swap(scoped_thread& x)BOOST_NOEXCEPT
> {
> t.swap(x.t);
> }
> void detach()
> {
> t.detach();
> }
> void join()
> {
> t.join();
> }
> template <class Rep, class Period>
> bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
> {
> return t.try_join_for(rel_time);
> }
> template <class Clock, class Duration>
> bool try_join_until(const chrono::time_point<Clock, Duration>&
> abs_time)
> {
> return t.try_join_until(abs_time);
> }
> thread::native_handle_type native_handle() BOOST_NOEXCEPT {
> return t.native_handle();
> }
> void interrupt()
> {
> t.interrupt();
> }
>
> bool interruption_requested() const BOOST_NOEXCEPT
> {
> return t.interruption_requested();
> }
> ~scoped_thread()
> {
> t.interrupt();
> if (t.joinable())
> {
> t.join();
> }
> }
> };
> inline void swap(scoped_thread& lhs,scoped_thread& rhs) BOOST_NOEXCEPT
> {
> return lhs.swap(rhs);
> }
> }
> }}}

New description:

 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, join it if joinable.

 While 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.

 {{{
 namespace boost
 {
   class scoped_thread : public thread
   {
     typedef thread base_type;
   public:
     BOOST_THREAD_MOVABLE_ONLY( scoped_thread )

     explicit scoped_thread(BOOST_THREAD_RV_REF(thread) t_) :
       base_type(boost::forward<base_type>(t_)),
       m_(*static_cast<base_type*>(this))
     {
     }

     scoped_thread(BOOST_RV_REF(scoped_thread) x) :
       base_type(boost::move(static_cast<base_type&>(x)))
     { }

     scoped_thread& operator=(BOOST_RV_REF(scoped_thread) x)
     {
       base_type::operator=(boost::move(static_cast<base_type&>(x)));
       return *this;
     }

     ~scoped_thread()
     {
       if (joinable())
       {
         join();
       }
     }
   };
   inline void swap(scoped_thread& lhs,scoped_thread& rhs) BOOST_NOEXCEPT
   {
       return lhs.swap(rhs);
   }
 }
 }}}

--
-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/7541#comment:3>
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:11 UTC