2
0
mirror of https://github.com/boostorg/thread.git synced 2026-02-08 23:22:13 +00:00
Files
thread/test/test_completion_latch.cpp
Andrey Semashev ee609e8806 Cleanup header includes.
1. Make inclusion of boost/bind/bind.hpp conditional in some cases, when the
   code actually conditionally uses boost::bind. Reduces compile-time overhead
   and fixes https://github.com/boostorg/thread/issues/307.

2. Remove some unnecessary uses of boost::ref. This allows to avoid including
   boost/core/ref.hpp in a few places, and avoids the associated template
   instantiation overhead in others.

3. Replace deprecated header includes with the more recent alternatives. For
   example: boost/detail/lightweight_test.hpp -> boost/core/lightweight_test.hpp,
   boost/ref.hpp -> boost/core/ref.hpp.

4. Replace some blanket includes with the more fine-grained ones. For example,
   boost/utility.hpp, boost/atomic.hpp. This reduces compile time overhead.

5. Add some missing includes, for example, boost/core/ref.hpp and
   boost/type_traits/is_same.hpp.

6. Replace uses of std::is_same with boost::is_same (with the corresponding
   included header) since the standard type_traits header presence and validity
   is not tested by the code. Using boost::is_same makes the code more portable.
2020-04-05 01:51:58 +03:00

109 lines
2.3 KiB
C++

// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// (C) Copyright 2013 Vicente J. Botet Escriba
#define BOOST_THREAD_PROVIDES_INTERRUPTIONS
#include <boost/thread/detail/config.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/completion_latch.hpp>
#include <boost/core/lightweight_test.hpp>
#include <vector>
namespace
{
// Shared variables for generation completion_latch test
const int N_THREADS = 10;
boost::completion_latch gen_latch(N_THREADS);
boost::mutex mutex;
long global_parameter;
void latch_thread()
{
{
boost::unique_lock<boost::mutex> lock(mutex);
global_parameter++;
}
gen_latch.count_down();
//do something else
}
} // namespace
void test_global_parameter()
{
boost::unique_lock<boost::mutex> lock(mutex);
BOOST_TEST_EQ(global_parameter, N_THREADS);
}
void reset_gen_latch()
{
{
boost::unique_lock<boost::mutex> lock(mutex);
BOOST_TEST_EQ(global_parameter, N_THREADS);
}
gen_latch.reset(N_THREADS);
}
void test_completion_latch_reset()
{
boost::thread_group g;
boost::thread_group g2;
gen_latch.then(&reset_gen_latch);
{
global_parameter = 0;
try
{
for (int i = 0; i < N_THREADS; ++i)
g.create_thread(&latch_thread);
if (!gen_latch.try_wait())
if (gen_latch.wait_for(boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
if (gen_latch.wait_until(boost::chrono::steady_clock::now()+boost::chrono::milliseconds(100)) == boost::cv_status::timeout)
gen_latch.wait(); // All the threads have been updated the global_parameter
g.join_all();
}
catch (...)
{
BOOST_TEST(false);
g.interrupt_all();
g.join_all();
//throw;
}
}
gen_latch.then(&test_global_parameter);
{
global_parameter = 0;
try
{
for (int i = 0; i < N_THREADS; ++i)
g2.create_thread(&latch_thread);
if (!gen_latch.try_wait())
gen_latch.wait(); // All the threads have been updated the global_parameter
g2.join_all();
}
catch (...)
{
BOOST_TEST(false);
g2.interrupt_all();
g2.join_all();
//throw;
}
}
}
int main()
{
test_completion_latch_reset();
return boost::report_errors();
}