1// Distributed under the Boost Software License, Version 1.0. (See
2// accompanying file LICENSE_1_0.txt or copy at
3// http://www.boost.org/LICENSE_1_0.txt)
4// (C) Copyright 2013 Vicente J. Botet Escriba
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/latch.hpp>
12
13#include <boost/detail/lightweight_test.hpp>
14#include <vector>
15
16namespace
17{
18
19 // Shared variables for generation latch test
20 const int N_THREADS = 10;
21 boost::latch gen_latch(N_THREADS);
22 boost::mutex mutex;
23 long global_parameter;
24
25 void latch_thread()
26 {
27 {
28 boost::unique_lock<boost::mutex> lock(mutex);
29 global_parameter++;
30 }
31 gen_latch.count_down();
32 //do something else
33 }
34
35} // namespace
36
37void test_latch()
38{
39 boost::thread_group g;
40 global_parameter = 0;
41
42 try
43 {
44 for (int i = 0; i < N_THREADS; ++i)
45 g.create_thread(threadfunc: &latch_thread);
46
47 if (! gen_latch.try_wait())
48 if (gen_latch.wait_for(rel_time: boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
49 if (gen_latch.wait_until(abs_time: boost::chrono::steady_clock::now()+boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
50 gen_latch.wait(); // All the threads have been updated the global_parameter
51 BOOST_TEST_EQ(global_parameter, N_THREADS);
52
53 g.join_all();
54 }
55 catch (...)
56 {
57 BOOST_TEST(false);
58 g.interrupt_all();
59 g.join_all();
60 //throw;
61 }
62
63}
64
65int main()
66{
67 test_latch();
68 return boost::report_errors();
69}
70
71

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