Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2007-10-30 13:16:25


Author: anthonyw
Date: 2007-10-30 13:16:24 EDT (Tue, 30 Oct 2007)
New Revision: 40609
URL: http://svn.boost.org/trac/boost/changeset/40609

Log:
Added specialization for reference_wrapper to allow use of boost::ref with boost::thread (again)
Text files modified:
   trunk/boost/thread/pthread/thread.hpp | 16 ++++++++++++++++
   trunk/boost/thread/win32/thread.hpp | 18 ++++++++++++++++++
   trunk/libs/thread/test/test_thread.cpp | 33 +++++++++++++++++++++++++++++++++
   3 files changed, 67 insertions(+), 0 deletions(-)

Modified: trunk/boost/thread/pthread/thread.hpp
==============================================================================
--- trunk/boost/thread/pthread/thread.hpp (original)
+++ trunk/boost/thread/pthread/thread.hpp 2007-10-30 13:16:24 EDT (Tue, 30 Oct 2007)
@@ -195,6 +195,22 @@
         bool cancellation_requested() const;
     };
 
+ template<typename F>
+ struct thread::thread_data<boost::reference_wrapper<F> >:
+ detail::thread_data_base
+ {
+ F& f;
+
+ thread_data(boost::reference_wrapper<F> f_):
+ f(f_)
+ {}
+
+ void run()
+ {
+ f();
+ }
+ };
+
     namespace this_thread
     {
         class BOOST_THREAD_DECL disable_cancellation

Modified: trunk/boost/thread/win32/thread.hpp
==============================================================================
--- trunk/boost/thread/win32/thread.hpp (original)
+++ trunk/boost/thread/win32/thread.hpp 2007-10-30 13:16:24 EDT (Tue, 30 Oct 2007)
@@ -17,6 +17,7 @@
 #include <boost/utility.hpp>
 #include <list>
 #include <algorithm>
+#include <boost/ref.hpp>
 
 namespace boost
 {
@@ -153,6 +154,23 @@
         static thread self();
     };
 
+ template<typename F>
+ struct thread::thread_data<boost::reference_wrapper<F> >:
+ detail::thread_data_base
+ {
+ F& f;
+
+ thread_data(boost::reference_wrapper<F> f_):
+ f(f_)
+ {}
+
+ void run()
+ {
+ f();
+ }
+ };
+
+
     namespace this_thread
     {
         class BOOST_THREAD_DECL disable_cancellation

Modified: trunk/libs/thread/test/test_thread.cpp
==============================================================================
--- trunk/libs/thread/test/test_thread.cpp (original)
+++ trunk/libs/thread/test/test_thread.cpp 2007-10-30 13:16:24 EDT (Tue, 30 Oct 2007)
@@ -9,6 +9,8 @@
 #include <boost/thread/thread.hpp>
 #include <boost/thread/xtime.hpp>
 #include <boost/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/utility.hpp>
 
 #include <boost/test/unit_test.hpp>
 
@@ -118,6 +120,36 @@
     timed_test(&do_test_thread_no_cancel_if_cancels_disabled_at_cancellation_point, 1);
 }
 
+struct non_copyable_functor:
+ boost::noncopyable
+{
+ unsigned value;
+
+ non_copyable_functor():
+ value(0)
+ {}
+
+ void operator()()
+ {
+ value=999;
+ }
+};
+
+void do_test_creation_through_reference_wrapper()
+{
+ non_copyable_functor f;
+
+ boost::thread thrd(boost::ref(f));
+ thrd.join();
+ BOOST_CHECK_EQUAL(f.value, 999);
+}
+
+void test_creation_through_reference_wrapper()
+{
+ timed_test(&do_test_creation_through_reference_wrapper, 1);
+}
+
+
 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
 {
     boost::unit_test_framework::test_suite* test =
@@ -128,6 +160,7 @@
     test->add(BOOST_TEST_CASE(test_id_comparison));
     test->add(BOOST_TEST_CASE(test_thread_cancels_at_cancellation_point));
     test->add(BOOST_TEST_CASE(test_thread_no_cancel_if_cancels_disabled_at_cancellation_point));
+ test->add(BOOST_TEST_CASE(test_creation_through_reference_wrapper));
 
     return test;
 }


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