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
16namespace {
17
18
19// Shared variables for generation barrier test
20long global_parameter;
21const int N_THREADS=3;
22
23unsigned int size_fct() {
24 global_parameter++;
25 return N_THREADS;
26}
27
28boost::barrier gen_barrier(N_THREADS, &size_fct);
29
30void barrier_thread()
31{
32 for (int i = 0; i < 5; ++i)
33 {
34 gen_barrier.count_down_and_wait();
35 }
36}
37
38} // namespace
39
40void test_barrier()
41{
42 boost::thread_group g;
43 global_parameter = 0;
44
45 try
46 {
47 for (int i = 0; i < N_THREADS; ++i)
48 g.create_thread(threadfunc: &barrier_thread);
49 g.join_all();
50 }
51 catch(...)
52 {
53 BOOST_TEST(false);
54 g.interrupt_all();
55 g.join_all();
56 //throw;
57 }
58
59 BOOST_TEST(global_parameter==5);
60
61}
62
63int main()
64{
65
66 test_barrier();
67 return boost::report_errors();
68}
69
70

source code of boost/libs/thread/test/test_barrier_size_fct.cpp