Changeset 81079


Ignore:
Timestamp:
Oct 29, 2012, 12:36:06 AM (5 years ago)
Author:
viboes
Message:

Thread: update thread_guard to make it a template parameterized by thread functors

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/boost/thread/thread_guard.hpp

    r81074 r81079  
    1212#include <boost/thread/detail/delete.hpp>
    1313#include <boost/thread/detail/move.hpp>
     14#include <boost/thread/thread_functors.hpp>
    1415
    1516#include <boost/config/abi_prefix.hpp>
     
    2122   * Non-copyable RAII scoped thread guard joiner which join the thread if joinable when destroyed.
    2223   */
    23   class strict_thread_joiner
     24  template <class CallableThread = join_if_joinable>
     25  class thread_guard
    2426  {
    25     thread& t;
     27    thread& t_;
    2628  public:
    27     BOOST_THREAD_MOVABLE_ONLY( strict_thread_joiner)
     29    BOOST_THREAD_NO_COPYABLE( thread_guard)
    2830
    29     explicit strict_thread_joiner(thread& t_) :
    30     t(t_)
     31    explicit thread_guard(thread& t) :
     32    t_(t)
    3133    {
    3234    }
    33     ~strict_thread_joiner()
     35    ~thread_guard()
    3436    {
    35       if (t.joinable())
    36       {
    37         t.join();
    38       }
    39     }
    40   };
     37      CallableThread on_destructor;
    4138
    42   /**
    43    * MoveOnly RAII scoped thread guard joiner which join the thread if joinable when destroyed.
    44    */
    45   class thread_joiner
    46   {
    47     thread* t;
    48   public:
    49     BOOST_THREAD_MOVABLE_ONLY( thread_joiner)
    50 
    51     explicit thread_joiner(thread& t_) :
    52       t(&t_)
    53     {
    54     }
    55 
    56     thread_joiner(BOOST_RV_REF(thread_joiner) x) :
    57     t(x.t)
    58     {
    59       x.t = 0;
    60     }
    61 
    62     thread_joiner& operator=(BOOST_RV_REF(thread_joiner) x)
    63     {
    64       t = x.t;
    65       x.t = 0;
    66       return *this;
    67     }
    68 
    69     void swap(thread_joiner& rhs)BOOST_NOEXCEPT
    70     {
    71       thread* tmp=t;
    72       t = rhs.t;
    73       rhs.t = tmp;
    74     }
    75 
    76     ~thread_joiner()
    77     {
    78       if (t)
    79       {
    80         if (t->joinable())
    81         {
    82           t->join();
    83         }
    84       }
     39      on_destructor(t_);
    8540    }
    8641  };
  • trunk/libs/thread/example/thread_guard.cpp

    r81074 r81079  
    4040    func my_func(some_local_state);
    4141    boost::thread t(my_func);
    42     boost::strict_thread_joiner g(t);
     42    boost::thread_guard<> g(t);
    4343
    4444    do_something_in_current_thread();
Note: See TracChangeset for help on using the changeset viewer.