Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2008-03-03 05:52:45


Author: anthonyw
Date: 2008-03-03 05:52:44 EST (Mon, 03 Mar 2008)
New Revision: 43464
URL: http://svn.boost.org/trac/boost/changeset/43464

Log:
thread constructor now accepts up to three additional arguments to pass to thread function
Added:
   trunk/libs/thread/test/test_thread_launching.cpp (contents, props changed)
Text files modified:
   trunk/boost/thread/pthread/thread.hpp | 30 ++++++++++++++++++++++++++++--
   trunk/boost/thread/win32/thread.hpp | 30 ++++++++++++++++++++++++++++--
   trunk/libs/thread/test/Jamfile.v2 | 1 +
   3 files changed, 57 insertions(+), 4 deletions(-)

Modified: trunk/boost/thread/pthread/thread.hpp
==============================================================================
--- trunk/boost/thread/pthread/thread.hpp (original)
+++ trunk/boost/thread/pthread/thread.hpp 2008-03-03 05:52:44 EST (Mon, 03 Mar 2008)
@@ -21,6 +21,7 @@
 #include <boost/thread/detail/move.hpp>
 #include <boost/shared_ptr.hpp>
 #include "thread_data.hpp"
+#include <boost/bind.hpp>
 
 namespace boost
 {
@@ -133,6 +134,12 @@
         explicit thread(detail::thread_data_ptr data);
 
         detail::thread_data_ptr get_thread_info() const;
+
+ template<typename F>
+ static inline detail::thread_data_ptr make_thread_info(F f)
+ {
+ return detail::thread_data_ptr(new thread_data<F>(f));
+ }
         
     public:
         thread();
@@ -140,13 +147,32 @@
 
         template <class F>
         explicit thread(F f):
- thread_info(new thread_data<F>(f))
+ thread_info(make_thread_info(f))
         {
             start_thread();
         }
         template <class F>
         thread(detail::thread_move_t<F> f):
- thread_info(new thread_data<F>(f))
+ thread_info(make_thread_info(f))
+ {
+ start_thread();
+ }
+
+ template <class F,class A1>
+ thread(F f,A1 a1):
+ thread_info(make_thread_info(boost::bind<void>(f,a1)))
+ {
+ start_thread();
+ }
+ template <class F,class A1,class A2>
+ thread(F f,A1 a1,A2 a2):
+ thread_info(make_thread_info(boost::bind<void>(f,a1,a2)))
+ {
+ start_thread();
+ }
+ template <class F,class A1,class A2,class A3>
+ thread(F f,A1 a1,A2 a2,A3 a3):
+ thread_info(make_thread_info(boost::bind<void>(f,a1,a2,a3)))
         {
             start_thread();
         }

Modified: trunk/boost/thread/win32/thread.hpp
==============================================================================
--- trunk/boost/thread/win32/thread.hpp (original)
+++ trunk/boost/thread/win32/thread.hpp 2008-03-03 05:52:44 EST (Mon, 03 Mar 2008)
@@ -20,6 +20,7 @@
 #include <algorithm>
 #include <boost/ref.hpp>
 #include <boost/cstdint.hpp>
+#include <boost/bind.hpp>
 
 namespace boost
 {
@@ -189,19 +190,44 @@
         explicit thread(detail::thread_data_ptr data);
 
         detail::thread_data_ptr get_thread_info() const;
+
+ template<typename F>
+ static inline detail::thread_data_ptr make_thread_info(F f)
+ {
+ return detail::heap_new<thread_data<F> >(f);
+ }
     public:
         thread();
         ~thread();
 
         template <class F>
         explicit thread(F f):
- thread_info(detail::heap_new<thread_data<F> >(f))
+ thread_info(make_thread_info(f))
         {
             start_thread();
         }
         template <class F>
         thread(detail::thread_move_t<F> f):
- thread_info(detail::heap_new<thread_data<F> >(f))
+ thread_info(make_thread_info(f))
+ {
+ start_thread();
+ }
+
+ template <class F,class A1>
+ thread(F f,A1 a1):
+ thread_info(make_thread_info(boost::bind<void>(f,a1)))
+ {
+ start_thread();
+ }
+ template <class F,class A1,class A2>
+ thread(F f,A1 a1,A2 a2):
+ thread_info(make_thread_info(boost::bind<void>(f,a1,a2)))
+ {
+ start_thread();
+ }
+ template <class F,class A1,class A2,class A3>
+ thread(F f,A1 a1,A2 a2,A3 a3):
+ thread_info(make_thread_info(boost::bind<void>(f,a1,a2,a3)))
         {
             start_thread();
         }

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 (original)
+++ trunk/libs/thread/test/Jamfile.v2 2008-03-03 05:52:44 EST (Mon, 03 Mar 2008)
@@ -38,6 +38,7 @@
           [ thread-run test_thread_id.cpp ]
           [ thread-run test_hardware_concurrency.cpp ]
           [ thread-run test_thread_move.cpp ]
+ [ thread-run test_thread_launching.cpp ]
           [ thread-run test_move_function.cpp ]
           [ thread-run test_mutex.cpp ]
           [ thread-run test_condition_notify_one.cpp ]

Added: trunk/libs/thread/test/test_thread_launching.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/thread/test/test_thread_launching.cpp 2008-03-03 05:52:44 EST (Mon, 03 Mar 2008)
@@ -0,0 +1,164 @@
+// Copyright (C) 2007 Anthony Williams
+//
+// 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 <boost/thread/thread.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/ref.hpp>
+#include <boost/utility.hpp>
+#include <string>
+#include <vector>
+
+bool normal_function_called=false;
+
+void normal_function()
+{
+ normal_function_called=true;
+}
+
+void test_thread_function_no_arguments()
+{
+ boost::thread function(normal_function);
+ function.join();
+ BOOST_CHECK(normal_function_called);
+}
+
+struct callable_no_args
+{
+ static bool called;
+
+ void operator()() const
+ {
+ called=true;
+ }
+};
+
+bool callable_no_args::called=false;
+
+void test_thread_callable_object_no_arguments()
+{
+ boost::thread callable((callable_no_args()));
+ callable.join();
+ BOOST_CHECK(callable_no_args::called);
+}
+
+struct callable_noncopyable_no_args:
+ boost::noncopyable
+{
+ static bool called;
+
+ void operator()() const
+ {
+ called=true;
+ }
+};
+
+bool callable_noncopyable_no_args::called=false;
+
+void test_thread_callable_object_ref_no_arguments()
+{
+ callable_noncopyable_no_args func;
+
+ boost::thread callable(boost::ref(func));
+ callable.join();
+ BOOST_CHECK(callable_noncopyable_no_args::called);
+}
+
+struct callable_one_arg
+{
+ static bool called;
+ static int called_arg;
+
+ void operator()(int arg) const
+ {
+ called=true;
+ called_arg=arg;
+ }
+};
+
+bool callable_one_arg::called=false;
+int callable_one_arg::called_arg=0;
+
+void test_thread_callable_object_one_argument()
+{
+ boost::thread callable(callable_one_arg(),42);
+ callable.join();
+ BOOST_CHECK(callable_one_arg::called);
+ BOOST_CHECK_EQUAL(callable_one_arg::called_arg,42);
+}
+
+struct callable_multiple_arg
+{
+ static bool called_two;
+ static int called_two_arg1;
+ static double called_two_arg2;
+ static bool called_three;
+ static std::string called_three_arg1;
+ static std::vector<int> called_three_arg2;
+ static int called_three_arg3;
+
+ void operator()(int arg1,double arg2) const
+ {
+ called_two=true;
+ called_two_arg1=arg1;
+ called_two_arg2=arg2;
+ }
+ void operator()(std::string const& arg1,std::vector<int> const& arg2,int arg3) const
+ {
+ called_three=true;
+ called_three_arg1=arg1;
+ called_three_arg2=arg2;
+ called_three_arg3=arg3;
+ }
+};
+
+bool callable_multiple_arg::called_two=false;
+bool callable_multiple_arg::called_three=false;
+int callable_multiple_arg::called_two_arg1;
+double callable_multiple_arg::called_two_arg2;
+std::string callable_multiple_arg::called_three_arg1;
+std::vector<int> callable_multiple_arg::called_three_arg2;
+int callable_multiple_arg::called_three_arg3;
+
+void test_thread_callable_object_multiple_arguments()
+{
+ std::vector<int> x;
+ for(unsigned i=0;i<7;++i)
+ {
+ x.push_back(i*i);
+ }
+
+ boost::thread callable3(callable_multiple_arg(),"hello",x,1.2);
+ callable3.join();
+ BOOST_CHECK(callable_multiple_arg::called_three);
+ BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg1,"hello");
+ BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg2.size(),x.size());
+ for(unsigned j=0;j<x.size();++j)
+ {
+ BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg2.at(j),x[j]);
+ }
+
+ BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg3,1);
+
+ double const dbl=1.234;
+
+ boost::thread callable2(callable_multiple_arg(),19,dbl);
+ callable2.join();
+ BOOST_CHECK(callable_multiple_arg::called_two);
+ BOOST_CHECK_EQUAL(callable_multiple_arg::called_two_arg1,19);
+ BOOST_CHECK_EQUAL(callable_multiple_arg::called_two_arg2,dbl);
+}
+
+
+boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
+{
+ boost::unit_test_framework::test_suite* test =
+ BOOST_TEST_SUITE("Boost.Threads: thread launching test suite");
+
+ test->add(BOOST_TEST_CASE(test_thread_function_no_arguments));
+ test->add(BOOST_TEST_CASE(test_thread_callable_object_no_arguments));
+ test->add(BOOST_TEST_CASE(test_thread_callable_object_ref_no_arguments));
+ test->add(BOOST_TEST_CASE(test_thread_callable_object_one_argument));
+ test->add(BOOST_TEST_CASE(test_thread_callable_object_multiple_arguments));
+ 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