|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r81074 - in trunk: boost/thread libs/thread/example
From: vicente.botet_at_[hidden]
Date: 2012-10-28 13:55:33
Author: viboes
Date: 2012-10-28 13:55:32 EDT (Sun, 28 Oct 2012)
New Revision: 81074
URL: http://svn.boost.org/trac/boost/changeset/81074
Log:
Thread: Added thread_joiner
Added:
trunk/boost/thread/thread_guard.hpp (contents, props changed)
trunk/libs/thread/example/thread_guard.cpp (contents, props changed)
Added: trunk/boost/thread/thread_guard.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/thread/thread_guard.hpp 2012-10-28 13:55:32 EDT (Sun, 28 Oct 2012)
@@ -0,0 +1,91 @@
+// 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 thread_joiner in CCiA
+
+#ifndef BOOST_THREAD_THREAD_GUARD_HPP
+#define BOOST_THREAD_THREAD_GUARD_HPP
+
+#include <boost/thread/detail/delete.hpp>
+#include <boost/thread/detail/move.hpp>
+
+#include <boost/config/abi_prefix.hpp>
+
+namespace boost
+{
+
+ /**
+ * Non-copyable RAII scoped 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();
+ }
+ }
+ };
+
+ /**
+ * 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_)
+ {
+ }
+
+ 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();
+ }
+ }
+ }
+ };
+
+}
+#include <boost/config/abi_suffix.hpp>
+
+#endif
Added: trunk/libs/thread/example/thread_guard.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/example/thread_guard.cpp 2012-10-28 13:55:32 EDT (Sun, 28 Oct 2012)
@@ -0,0 +1,53 @@
+// (C) Copyright 2009-2012 Anthony Williams
+// (C) Copyright 2012 Vicente Botet
+//
+// 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)
+
+
+#include <iostream>
+#include <string>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/thread_guard.hpp>
+
+void do_something(int& i)
+{
+ ++i;
+}
+
+struct func
+{
+ int& i;
+
+ func(int& i_):i(i_){}
+
+ void operator()()
+ {
+ for(unsigned j=0;j<1000000;++j)
+ {
+ do_something(i);
+ }
+ }
+};
+
+void do_something_in_current_thread()
+{}
+
+
+void f()
+{
+ int some_local_state;
+ func my_func(some_local_state);
+ boost::thread t(my_func);
+ boost::strict_thread_joiner g(t);
+
+ do_something_in_current_thread();
+}
+
+int main()
+{
+ f();
+ return 0;
+}
+
+
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