Boost logo

Boost :

Subject: Re: [boost] [thread] terminating destructor
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2012-10-26 03:53:42


2012/10/25 Vicente J. Botet Escriba <vicente.botet_at_[hidden]>

>
> I've been working a little bit these classes and I have not find that
> interrupt-and-join on the destructor could be a a good compromise. As
> others, maybe you, have signaled, each developer could need a specific
> action on the destruction. As always been more generic means making the
> class parameterized with a policy. A possible implementation could be
>
>
> /**
> * Non-copyable RAII strict thread guard joiner which join the thread if
> joinable when destroyed.
> */
> class strict_thread_joiner
> {
> thread& t;
> public:
> BOOST_THREAD_MOVABLE_ONLY( strict_thread_joiner)
>
> explicit strict_thread_joiner(thread& t_) :
> t(t_)
> {
> }
> ~strict_thread_joiner()
> {
> if (t.joinable())
> {
> t.join();
> }
> }
> };
>
> /**
> * RAI @c thread wrapper adding a specific StrictThreadGuard allowing to
> master what can be done at destruction time.
> *
> * StrictThreadGuard: A NonCopyable class that takes a @c thread& as
> constructor parameter.
> * The default is a strict thread joiner guard.
> *
> * thread std::thread destructor terminates the program if the thread is
> not joinable.
> * Having a wrapper that can join the thread before destroying it seems
> a natural need.
> *
> * Example:
> *
> * boost::strict_scoped_thread<> t((boost::thread(F)));
> *
> */
> template <class StrictThreadGuard = strict_thread_joiner>
> class strict_scoped_thread
> {
> thread t_;
> StrictThreadGuard m_;
> public:
>
> BOOST_THREAD_NO_COPYABLE( strict_scoped_thread ) /// non copyable
>
> /**
> * Constructor from the thread to own.
> *
> * @param t: the thread to own.
> *
> * Effects: move the thread to own @c t and pass the stored thread to
> an internal Destroyer.
> */
> explicit strict_scoped_thread(BOOST_**THREAD_RV_REF(thread) t) :
> t_(boost::move(t)),
> m_(t_)
> {
> }
> };
>
>
> Now it is up to the user to change the default strict_thread_joiner policy.
>
> I've implemented also a non-strict thread wrapper that behaves a thread
> except that for the destruction.
> Maybe need another name.
>
> /**
> * RAI @c thread wrapper adding a specific destroyer allowing to master
> what can be done at destruction time.
> *
> * ThreadGuard: A MoveOnly class that takes a @c thread& as constructor
> parameter.
> * The default is a thread_joiner guard.
> *
> * thread std::thread destructor terminates the program if the thread is
> not joinable.
> * Having a wrapper that can join the thread before destroying it seems
> a natural need.
> *
> * Remark: @c scoped_thread is not a @c thread as @c thread is not
> designed to be derived from as a polymorphic type.
> * Anyway @c scoped_thread can be used in most of the contexts a @c
> thread could be used as it has the
> * same non-deprecated interface with the exception of the construction.
> *
> * Example:
> *
> * boost::scoped_thread<> t((boost::thread(F)));
> * t.interrupt();
> *
> */
> template <class ThreadGuard = thread_joiner>
> class scoped_thread;
>
> Does this respond to your question?

It definitely does!
Just one suggestion, wouldn't it be better if the policy was only required
to be callable with the argument of type thread& ? I.g.:

  struct strict_thread_joiner
  {
    void operator()( thread& t ) const
    {
      if (t.joinable()) {
        t.join();
      }
    }
  };

Regards,
&rzej


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk