• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //          Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // This test is based on the tests of Boost.Thread
8 
9 #include <sstream>
10 #include <string>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/fiber/all.hpp>
15 
16 int value1 = 0;
17 int value2 = 0;
18 
fn1(boost::fibers::barrier & b)19 void fn1( boost::fibers::barrier & b) {
20     ++value1;
21     boost::this_fiber::yield();
22 
23     b.wait();
24 
25     ++value1;
26     boost::this_fiber::yield();
27     ++value1;
28     boost::this_fiber::yield();
29     ++value1;
30     boost::this_fiber::yield();
31     ++value1;
32 }
33 
fn2(boost::fibers::barrier & b)34 void fn2( boost::fibers::barrier & b) {
35     ++value2;
36     boost::this_fiber::yield();
37     ++value2;
38     boost::this_fiber::yield();
39     ++value2;
40     boost::this_fiber::yield();
41 
42     b.wait();
43 
44     ++value2;
45     boost::this_fiber::yield();
46     ++value2;
47 }
48 
test_barrier()49 void test_barrier() {
50     value1 = 0;
51     value2 = 0;
52 
53     boost::fibers::barrier b( 2);
54     boost::fibers::fiber f1( boost::fibers::launch::dispatch, fn1, std::ref( b) );
55     boost::fibers::fiber f2( boost::fibers::launch::dispatch, fn2, std::ref( b) );
56 
57     f1.join();
58     f2.join();
59 
60     BOOST_CHECK_EQUAL( 5, value1);
61     BOOST_CHECK_EQUAL( 5, value2);
62 }
63 
init_unit_test_suite(int,char * [])64 boost::unit_test::test_suite * init_unit_test_suite( int, char* []) {
65     boost::unit_test::test_suite * test =
66         BOOST_TEST_SUITE("Boost.Fiber: barrier test suite");
67 
68     test->add( BOOST_TEST_CASE( & test_barrier) );
69 
70     return test;
71 }
72