Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81079 - in trunk: boost/thread libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2012-10-28 20:36:07


Author: viboes
Date: 2012-10-28 20:36:06 EDT (Sun, 28 Oct 2012)
New Revision: 81079
URL: http://svn.boost.org/trac/boost/changeset/81079

Log:
Thread: update thread_guard to make it a template parameterized by thread functors
Added:
   trunk/boost/thread/thread_functors.hpp (contents, props changed)
Text files modified:
   trunk/boost/thread/thread_guard.hpp | 65 ++++++---------------------------------
   trunk/libs/thread/example/thread_guard.cpp | 2
   2 files changed, 11 insertions(+), 56 deletions(-)

Added: trunk/boost/thread/thread_functors.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/thread/thread_functors.hpp 2012-10-28 20:36:06 EDT (Sun, 28 Oct 2012)
@@ -0,0 +1,56 @@
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+// (C) Copyright 2009-2012 Anthony Williams
+// (C) Copyright 2012 Vicente J. Botet Escriba
+
+// Based on the Anthony's idea of scoped_thread in CCiA
+
+#ifndef BOOST_THREAD_THREAD_FUNCTORS_HPP
+#define BOOST_THREAD_THREAD_FUNCTORS_HPP
+
+#include <boost/thread/detail/config.hpp>
+#include <boost/thread/detail/delete.hpp>
+#include <boost/thread/detail/move.hpp>
+#include <boost/thread/thread.hpp>
+
+#include <boost/config/abi_prefix.hpp>
+
+namespace boost
+{
+
+ struct detach
+ {
+ void operator()(thread& t)
+ {
+ t.detach();
+ }
+ };
+
+ struct join_if_joinable
+ {
+ void operator()(thread& t)
+ {
+ if (t.joinable())
+ {
+ t.join();
+ }
+ }
+ };
+
+ struct interrupt_and_join_if_joinable
+ {
+ void operator()(thread& t)
+ {
+ t.interrupt();
+ if (t.joinable())
+ {
+ t.join();
+ }
+ }
+ };
+
+}
+#include <boost/config/abi_suffix.hpp>
+
+#endif

Modified: trunk/boost/thread/thread_guard.hpp
==============================================================================
--- trunk/boost/thread/thread_guard.hpp (original)
+++ trunk/boost/thread/thread_guard.hpp 2012-10-28 20:36:06 EDT (Sun, 28 Oct 2012)
@@ -11,6 +11,7 @@
 
 #include <boost/thread/detail/delete.hpp>
 #include <boost/thread/detail/move.hpp>
+#include <boost/thread/thread_functors.hpp>
 
 #include <boost/config/abi_prefix.hpp>
 
@@ -20,68 +21,22 @@
   /**
    * Non-copyable RAII scoped thread guard joiner which join the thread if joinable when destroyed.
    */
- class strict_thread_joiner
+ template <class CallableThread = join_if_joinable>
+ class thread_guard
   {
- thread& t;
+ thread& t_;
   public:
- BOOST_THREAD_MOVABLE_ONLY( strict_thread_joiner)
+ BOOST_THREAD_NO_COPYABLE( thread_guard)
 
- explicit strict_thread_joiner(thread& t_) :
- t(t_)
+ explicit thread_guard(thread& t) :
+ t_(t)
     {
     }
- ~strict_thread_joiner()
+ ~thread_guard()
     {
- if (t.joinable())
- {
- t.join();
- }
- }
- };
-
- /**
- * MoveOnly RAII scoped thread guard joiner which join the thread if joinable when destroyed.
- */
- class thread_joiner
- {
- thread* t;
- public:
- BOOST_THREAD_MOVABLE_ONLY( thread_joiner)
-
- explicit thread_joiner(thread& t_) :
- t(&t_)
- {
- }
+ CallableThread on_destructor;
 
- thread_joiner(BOOST_RV_REF(thread_joiner) x) :
- t(x.t)
- {
- x.t = 0;
- }
-
- thread_joiner& operator=(BOOST_RV_REF(thread_joiner) x)
- {
- t = x.t;
- x.t = 0;
- return *this;
- }
-
- void swap(thread_joiner& rhs)BOOST_NOEXCEPT
- {
- thread* tmp=t;
- t = rhs.t;
- rhs.t = tmp;
- }
-
- ~thread_joiner()
- {
- if (t)
- {
- if (t->joinable())
- {
- t->join();
- }
- }
+ on_destructor(t_);
     }
   };
 

Modified: trunk/libs/thread/example/thread_guard.cpp
==============================================================================
--- trunk/libs/thread/example/thread_guard.cpp (original)
+++ trunk/libs/thread/example/thread_guard.cpp 2012-10-28 20:36:06 EDT (Sun, 28 Oct 2012)
@@ -39,7 +39,7 @@
     int some_local_state;
     func my_func(some_local_state);
     boost::thread t(my_func);
- boost::strict_thread_joiner g(t);
+ boost::thread_guard<> g(t);
 
     do_something_in_current_thread();
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk