• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Gennadiy Rozental 2008-2015.
2 //  Distributed under the Boost Software License, Version 1.0.
3 //  (See accompanying file LICENSE_1_0.txt or copy at
4 //  http://www.boost.org/LICENSE_1_0.txt)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //  File        : $RCSfile$
9 //
10 //  Version     : $Revision$
11 //
12 //  Description : tests Unit Test Framework usability in MT environment with
13 //                Boost.Test calls externally synchronized
14 // ***************************************************************************
15 
16 #define BOOST_TEST_MODULE sync_access_test
17 #include <boost/test/unit_test.hpp>
18 
19 #include <boost/thread.hpp>
20 #include <boost/thread/barrier.hpp>
21 #include <boost/bind/bind.hpp>
22 #include <boost/ref.hpp>
23 
24 using namespace boost;
25 namespace ut = boost::unit_test;
26 
27 static boost::mutex m;
28 
29 /// thread execution function
thread_function(boost::barrier & b)30 static void thread_function(boost::barrier& b)
31 {
32     b.wait(); /// wait until memory barrier allows the execution
33     boost::mutex::scoped_lock lock(m); /// lock mutex
34     BOOST_TEST(1 ==0 ); /// produce the fault
35 }
36 
37 #if BOOST_PP_VARIADICS
38 /// test function which creates threads
39 BOOST_AUTO_TEST_CASE( test_multiple_assertion_faults, * ut::expected_failures(100))
40 #else
41 BOOST_TEST_DECORATOR(
42   * ut::expected_failures(100)
43 )
44 BOOST_AUTO_TEST_CASE( test_multiple_assertion_faults)
45 #endif
46 {
47     boost::thread_group tg;      // thread group to manage all threads
48     boost::barrier      b(100);  // memory barrier, which should block all threads
49                                  // until all 100 threads were created
50 
51     for(size_t i=0; i<100; ++i)
52         tg.create_thread(boost::bind(thread_function, ref(b))); /// create a thread and pass it the barrier
53 
54     tg.join_all();
55 }
56 
57 // EOF
58