1 | // (C) Copyright 2013 Vicente J. Botet Escriba |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #define BOOST_THREAD_PROVIDES_INTERRUPTIONS |
7 | |
8 | #include <boost/thread/detail/config.hpp> |
9 | |
10 | #include <boost/thread/thread.hpp> |
11 | #include <boost/thread/barrier.hpp> |
12 | |
13 | #include <boost/detail/lightweight_test.hpp> |
14 | #include <vector> |
15 | |
16 | namespace { |
17 | |
18 | |
19 | // Shared variables for generation barrier test |
20 | long global_parameter; |
21 | |
22 | void void_fct() { |
23 | global_parameter++; |
24 | } |
25 | |
26 | const int N_THREADS=3; |
27 | boost::barrier gen_barrier(N_THREADS, &void_fct); |
28 | |
29 | void barrier_thread() |
30 | { |
31 | for (int i = 0; i < 5; ++i) |
32 | { |
33 | gen_barrier.count_down_and_wait(); |
34 | } |
35 | } |
36 | |
37 | } // namespace |
38 | |
39 | void test_barrier() |
40 | { |
41 | boost::thread_group g; |
42 | global_parameter = 0; |
43 | |
44 | try |
45 | { |
46 | for (int i = 0; i < N_THREADS; ++i) |
47 | g.create_thread(threadfunc: &barrier_thread); |
48 | g.join_all(); |
49 | } |
50 | catch(...) |
51 | { |
52 | BOOST_TEST(false); |
53 | g.interrupt_all(); |
54 | g.join_all(); |
55 | //throw; |
56 | } |
57 | |
58 | BOOST_TEST(global_parameter==5); |
59 | |
60 | } |
61 | |
62 | int main() |
63 | { |
64 | |
65 | test_barrier(); |
66 | return boost::report_errors(); |
67 | } |
68 | |
69 |