Boost logo

Boost-Commit :

From: anthony_at_[hidden]
Date: 2007-10-08 05:48:57


Author: anthonyw
Date: 2007-10-08 05:48:57 EDT (Mon, 08 Oct 2007)
New Revision: 39780
URL: http://svn.boost.org/trac/boost/changeset/39780

Log:
added extended test for new call_once
Text files modified:
   trunk/libs/thread/test/test_once.cpp | 49 ++++++++++++++++++++++++++++++++++++++++
   1 files changed, 49 insertions(+), 0 deletions(-)

Modified: trunk/libs/thread/test/test_once.cpp
==============================================================================
--- trunk/libs/thread/test/test_once.cpp (original)
+++ trunk/libs/thread/test/test_once.cpp 2007-10-08 05:48:57 EDT (Mon, 08 Oct 2007)
@@ -49,6 +49,54 @@
     BOOST_CHECK_EQUAL(var_to_init,1);
 }
 
+int var_to_init_with_functor=0;
+
+struct increment_value
+{
+ int* value;
+ explicit increment_value(int* value_):
+ value(value_)
+ {}
+
+ void operator()() const
+ {
+ boost::mutex::scoped_lock lock(m);
+ ++(*value);
+ }
+};
+
+void call_once_with_functor()
+{
+ unsigned const loop_count=100;
+ int my_once_value=0;
+ static boost::once_flag functor_flag=BOOST_ONCE_INIT;
+ for(unsigned i=0;i<loop_count;++i)
+ {
+ boost::call_once(functor_flag, increment_value(&var_to_init_with_functor));
+ my_once_value=var_to_init_with_functor;
+ if(my_once_value!=1)
+ {
+ break;
+ }
+ }
+ boost::mutex::scoped_lock lock(m);
+ BOOST_CHECK_EQUAL(my_once_value, 1);
+}
+
+
+void test_call_once_arbitrary_functor()
+{
+ unsigned const num_threads=100;
+ boost::thread_group group;
+
+ for(unsigned i=0;i<num_threads;++i)
+ {
+ group.create_thread(&call_once_with_functor);
+ }
+ group.join_all();
+ BOOST_CHECK_EQUAL(var_to_init_with_functor,1);
+}
+
 
 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
 {
@@ -56,6 +104,7 @@
         BOOST_TEST_SUITE("Boost.Threads: call_once test suite");
 
     test->add(BOOST_TEST_CASE(test_call_once));
+ test->add(BOOST_TEST_CASE(test_call_once_arbitrary_functor));
 
     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