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
6
#define BOOST_THREAD_PROVIDES_INTERRUPTIONS
8
#include <boost/thread/detail/config.hpp>
10
#include <boost/thread/thread.hpp>
11
#include <boost/thread/completion_latch.hpp>
13
#include <boost/detail/lightweight_test.hpp>
19
// Shared variables for generation completion_latch test
20
const int N_THREADS = 10;
21
boost::completion_latch gen_latch(N_THREADS);
23
long global_parameter;
28
boost::unique_lock<boost::mutex> lock(mutex);
31
gen_latch.count_down();
37
void test_global_parameter()
39
boost::unique_lock<boost::mutex> lock(mutex);
40
BOOST_TEST_EQ(global_parameter, N_THREADS);
43
void reset_gen_latch()
46
boost::unique_lock<boost::mutex> lock(mutex);
47
BOOST_TEST_EQ(global_parameter, N_THREADS);
49
gen_latch.reset(N_THREADS);
52
void test_completion_latch_reset()
54
boost::thread_group g;
55
boost::thread_group g2;
57
gen_latch.then(&reset_gen_latch);
63
for (int i = 0; i < N_THREADS; ++i)
64
g.create_thread(&latch_thread);
66
if (!gen_latch.try_wait())
67
if (gen_latch.wait_for(boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
68
if (gen_latch.wait_until(boost::chrono::steady_clock::now()+boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
69
gen_latch.wait(); // All the threads have been updated the global_parameter
79
gen_latch.then(&test_global_parameter);
84
for (int i = 0; i < N_THREADS; ++i)
85
g2.create_thread(&latch_thread);
87
if (!gen_latch.try_wait())
88
gen_latch.wait(); // All the threads have been updated the global_parameter
103
test_completion_latch_reset();
104
return boost::report_errors();