Modify

Opened 5 years ago

Closed 5 years ago

#7541 closed Feature Requests (fixed)

Add a thread wrapper class that joins on destruction

Reported by: viboes Owned by: viboes
Milestone: Boost 1.53.0 Component: thread
Version: Boost 1.52.0 Severity: Cosmetic
Keywords: Cc:

Description (last modified by viboes)

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);
  }
}

Attachments (0)

Change History (8)

comment:1 Changed 5 years ago by viboes

  • Status changed from new to assigned

comment:2 Changed 5 years ago by viboes

  • Description modified (diff)

comment:3 Changed 5 years ago by viboes

  • Description modified (diff)

comment:4 Changed 5 years ago by viboes

  • Milestone To Be Determined deleted
  • Summary changed from Add a thread wrapper class that interrupts and join on destruction to Add a thread wrapper class that joins on destruction

comment:5 Changed 5 years ago by viboes

Committed revision [81073][81079][81080][81086].

Last edited 5 years ago by viboes (previous) (diff)

comment:6 Changed 5 years ago by viboes

Committed revision [81667].

comment:7 Changed 5 years ago by viboes

  • Milestone set to Boost 1.53.0

comment:8 Changed 5 years ago by viboes

  • Resolution set to fixed
  • Status changed from assigned to closed

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain viboes.
The resolution will be deleted. Next status will be 'reopened'.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.