mirror of
https://github.com/boostorg/thread.git
synced 2026-01-24 06:22:12 +00:00
* [@http://svn.boost.org/trac/boost/ticket/2309 #2309] Lack of g++ symbol visibility support in Boost.Thread. * [@http://svn.boost.org/trac/boost/ticket/2639 #2639] documentation should be extended(defer_lock, try_to_lock, ...). * [@http://svn.boost.org/trac/boost/ticket/3639 #3639] Boost.Thread doesn't build with Sun-5.9 on Linux. * [@http://svn.boost.org/trac/boost/ticket/3762 #3762] Thread can't be compiled with winscw (Codewarrior by Nokia). * [@http://svn.boost.org/trac/boost/ticket/3885 #3885] document about mix usage of boost.thread and native thread api. * [@http://svn.boost.org/trac/boost/ticket/3975 #3975] Incorrect precondition for promise::set_wait_callback(). * [@http://svn.boost.org/trac/boost/ticket/4048 #4048] thread::id formatting involves locale * [@http://svn.boost.org/trac/boost/ticket/4315 #4315] gcc 4.4 Warning: inline ... declared as dllimport: attribute ignored. * [@http://svn.boost.org/trac/boost/ticket/4480 #4480] OpenVMS patches for compiler issues workarounds. * [@http://svn.boost.org/trac/boost/ticket/4819 #4819] boost.thread's documentation misprints. * [@http://svn.boost.org/trac/boost/ticket/5423 #5423] thread issues with C++0x. * [@http://svn.boost.org/trac/boost/ticket/5617 #5617] boost::thread::id copy ctor. * [@http://svn.boost.org/trac/boost/ticket/5739 #5739] set-but-not-used warnings with gcc-4.6. * [@http://svn.boost.org/trac/boost/ticket/5826 #5826] threads.cpp: resource leak on threads creation failure. * [@http://svn.boost.org/trac/boost/ticket/5839 #5839] thread.cpp: ThreadProxy leaks on exceptions. * [@http://svn.boost.org/trac/boost/ticket/5859 #5859] win32 shared_mutex constructor leaks on exceptions. * [@http://svn.boost.org/trac/boost/ticket/6100 #6100] Compute hardware_concurrency() using get_nprocs() on GLIBC systems. * [@http://svn.boost.org/trac/boost/ticket/6168 #6168] recursive_mutex is using wrong config symbol (possible typo). * [@http://svn.boost.org/trac/boost/ticket/6175 #6175] Compile error with SunStudio. * [@http://svn.boost.org/trac/boost/ticket/6200 #6200] patch to have condition_variable and mutex error better handle EINTR. * [@http://svn.boost.org/trac/boost/ticket/6207 #6207] shared_lock swap compiler error on clang 3.0 c++11. * [@http://svn.boost.org/trac/boost/ticket/6208 #6208] try_lock_wrapper swap compiler error on clang 3.0 c++11. [SVN r76291]
191 lines
5.1 KiB
C++
191 lines
5.1 KiB
C++
// Copyright (C) 2001-2003
|
|
// William E. Kempf
|
|
// Copyright (C) 2007 Anthony Williams
|
|
//
|
|
// 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)
|
|
|
|
#include <boost/thread/detail/config.hpp>
|
|
|
|
#include <boost/thread/condition.hpp>
|
|
#include <boost/thread/thread.hpp>
|
|
#include <boost/thread/xtime.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <libs/thread/test/util.inl>
|
|
|
|
struct condition_test_data
|
|
{
|
|
condition_test_data() : notified(0), awoken(0) { }
|
|
|
|
boost::mutex mutex;
|
|
boost::condition_variable condition;
|
|
int notified;
|
|
int awoken;
|
|
};
|
|
|
|
void condition_test_thread(condition_test_data* data)
|
|
{
|
|
boost::mutex::scoped_lock lock(data->mutex);
|
|
BOOST_CHECK(lock ? true : false);
|
|
while (!(data->notified > 0))
|
|
data->condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
data->awoken++;
|
|
}
|
|
|
|
struct cond_predicate
|
|
{
|
|
cond_predicate(int& var, int val) : _var(var), _val(val) { }
|
|
|
|
bool operator()() { return _var == _val; }
|
|
|
|
int& _var;
|
|
int _val;
|
|
private:
|
|
void operator=(cond_predicate&);
|
|
|
|
};
|
|
|
|
void condition_test_waits(condition_test_data* data)
|
|
{
|
|
boost::mutex::scoped_lock lock(data->mutex);
|
|
BOOST_CHECK(lock ? true : false);
|
|
|
|
// Test wait.
|
|
while (data->notified != 1)
|
|
data->condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data->notified, 1);
|
|
data->awoken++;
|
|
data->condition.notify_one();
|
|
|
|
// Test predicate wait.
|
|
data->condition.wait(lock, cond_predicate(data->notified, 2));
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data->notified, 2);
|
|
data->awoken++;
|
|
data->condition.notify_one();
|
|
|
|
// Test timed_wait.
|
|
boost::xtime xt = delay(10);
|
|
while (data->notified != 3)
|
|
data->condition.timed_wait(lock, xt);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data->notified, 3);
|
|
data->awoken++;
|
|
data->condition.notify_one();
|
|
|
|
// Test predicate timed_wait.
|
|
xt = delay(10);
|
|
cond_predicate pred(data->notified, 4);
|
|
BOOST_CHECK(data->condition.timed_wait(lock, xt, pred));
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK(pred());
|
|
BOOST_CHECK_EQUAL(data->notified, 4);
|
|
data->awoken++;
|
|
data->condition.notify_one();
|
|
|
|
// Test predicate timed_wait with relative timeout
|
|
cond_predicate pred_rel(data->notified, 5);
|
|
BOOST_CHECK(data->condition.timed_wait(lock, boost::posix_time::seconds(10), pred_rel));
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK(pred_rel());
|
|
BOOST_CHECK_EQUAL(data->notified, 5);
|
|
data->awoken++;
|
|
data->condition.notify_one();
|
|
}
|
|
|
|
void do_test_condition_waits()
|
|
{
|
|
condition_test_data data;
|
|
|
|
boost::thread thread(bind(&condition_test_waits, &data));
|
|
|
|
{
|
|
boost::mutex::scoped_lock lock(data.mutex);
|
|
BOOST_CHECK(lock ? true : false);
|
|
|
|
boost::thread::sleep(delay(1));
|
|
data.notified++;
|
|
data.condition.notify_one();
|
|
while (data.awoken != 1)
|
|
data.condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data.awoken, 1);
|
|
|
|
boost::thread::sleep(delay(1));
|
|
data.notified++;
|
|
data.condition.notify_one();
|
|
while (data.awoken != 2)
|
|
data.condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data.awoken, 2);
|
|
|
|
boost::thread::sleep(delay(1));
|
|
data.notified++;
|
|
data.condition.notify_one();
|
|
while (data.awoken != 3)
|
|
data.condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data.awoken, 3);
|
|
|
|
boost::thread::sleep(delay(1));
|
|
data.notified++;
|
|
data.condition.notify_one();
|
|
while (data.awoken != 4)
|
|
data.condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data.awoken, 4);
|
|
|
|
|
|
boost::thread::sleep(delay(1));
|
|
data.notified++;
|
|
data.condition.notify_one();
|
|
while (data.awoken != 5)
|
|
data.condition.wait(lock);
|
|
BOOST_CHECK(lock ? true : false);
|
|
BOOST_CHECK_EQUAL(data.awoken, 5);
|
|
}
|
|
|
|
thread.join();
|
|
BOOST_CHECK_EQUAL(data.awoken, 5);
|
|
}
|
|
|
|
void test_condition_waits()
|
|
{
|
|
// We should have already tested notify_one here, so
|
|
// a timed test with the default execution_monitor::use_condition
|
|
// should be OK, and gives the fastest performance
|
|
timed_test(&do_test_condition_waits, 12);
|
|
}
|
|
|
|
void do_test_condition_wait_is_a_interruption_point()
|
|
{
|
|
condition_test_data data;
|
|
|
|
boost::thread thread(bind(&condition_test_thread, &data));
|
|
|
|
thread.interrupt();
|
|
thread.join();
|
|
BOOST_CHECK_EQUAL(data.awoken,0);
|
|
}
|
|
|
|
|
|
void test_condition_wait_is_a_interruption_point()
|
|
{
|
|
timed_test(&do_test_condition_wait_is_a_interruption_point, 1);
|
|
}
|
|
|
|
boost::unit_test::test_suite* init_unit_test_suite(int, char*[])
|
|
{
|
|
boost::unit_test::test_suite* test =
|
|
BOOST_TEST_SUITE("Boost.Threads: condition test suite");
|
|
|
|
test->add(BOOST_TEST_CASE(&test_condition_waits));
|
|
test->add(BOOST_TEST_CASE(&test_condition_wait_is_a_interruption_point));
|
|
|
|
return test;
|
|
}
|