• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2001-2003
2 // William E. Kempf
3 // (C) Copyright 2013 Vicente J. Botet Escriba
4 //
5 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
6 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 
8 #define BOOST_THREAD_PROVIDES_INTERRUPTIONS
9 
10 #include <boost/thread/detail/config.hpp>
11 
12 #include <boost/thread/thread.hpp>
13 #include <boost/thread/barrier.hpp>
14 
15 #include <boost/detail/lightweight_test.hpp>
16 #include <vector>
17 
18 namespace {
19 
20 // Shared variables for generation barrier test
21 const int N_THREADS=3;
22 boost::barrier gen_barrier(N_THREADS);
23 boost::mutex mutex;
24 long global_parameter;
25 
barrier_thread()26 void barrier_thread()
27 {
28     for (int i = 0; i < 5; ++i)
29     {
30         if (gen_barrier.wait())
31         {
32             boost::unique_lock<boost::mutex> lock(mutex);
33             global_parameter++;
34         }
35     }
36 }
37 
38 } // namespace
39 
test_barrier()40 void 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(&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 
main()63 int main()
64 {
65 
66     test_barrier();
67     return boost::report_errors();
68 }
69 
70