2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-23 18:12:12 +00:00
Files
thread/test/test_condition_notify_all.cpp
Vicente J. Botet Escriba 8affa33718 Thread fixed Bugs:
* [@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]
2012-01-03 17:31:50 +00:00

223 lines
5.3 KiB
C++

// 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/thread.hpp>
#include <boost/test/unit_test.hpp>
#include <libs/thread/test/util.inl>
#include "condition_test_common.hpp"
unsigned const number_of_test_threads=5;
void do_test_condition_notify_all_wakes_from_wait()
{
wait_for_flag data;
boost::thread_group group;
try
{
for(unsigned i=0;i<number_of_test_threads;++i)
{
group.create_thread(bind(&wait_for_flag::wait_without_predicate, data));
}
{
boost::mutex::scoped_lock lock(data.mutex);
data.flag=true;
data.cond_var.notify_all();
}
group.join_all();
BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
}
catch(...)
{
group.join_all();
throw;
}
}
void do_test_condition_notify_all_wakes_from_wait_with_predicate()
{
wait_for_flag data;
boost::thread_group group;
try
{
for(unsigned i=0;i<number_of_test_threads;++i)
{
group.create_thread(bind(&wait_for_flag::wait_with_predicate, data));
}
{
boost::mutex::scoped_lock lock(data.mutex);
data.flag=true;
data.cond_var.notify_all();
}
group.join_all();
BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
}
catch(...)
{
group.join_all();
throw;
}
}
void do_test_condition_notify_all_wakes_from_timed_wait()
{
wait_for_flag data;
boost::thread_group group;
try
{
for(unsigned i=0;i<number_of_test_threads;++i)
{
group.create_thread(bind(&wait_for_flag::timed_wait_without_predicate, data));
}
{
boost::mutex::scoped_lock lock(data.mutex);
data.flag=true;
data.cond_var.notify_all();
}
group.join_all();
BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
}
catch(...)
{
group.join_all();
throw;
}
}
void do_test_condition_notify_all_wakes_from_timed_wait_with_predicate()
{
wait_for_flag data;
boost::thread_group group;
try
{
for(unsigned i=0;i<number_of_test_threads;++i)
{
group.create_thread(bind(&wait_for_flag::timed_wait_with_predicate, data));
}
{
boost::mutex::scoped_lock lock(data.mutex);
data.flag=true;
data.cond_var.notify_all();
}
group.join_all();
BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
}
catch(...)
{
group.join_all();
throw;
}
}
void do_test_condition_notify_all_wakes_from_relative_timed_wait_with_predicate()
{
wait_for_flag data;
boost::thread_group group;
try
{
for(unsigned i=0;i<number_of_test_threads;++i)
{
group.create_thread(bind(&wait_for_flag::relative_timed_wait_with_predicate, data));
}
{
boost::mutex::scoped_lock lock(data.mutex);
data.flag=true;
data.cond_var.notify_all();
}
group.join_all();
BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
}
catch(...)
{
group.join_all();
throw;
}
}
namespace
{
boost::mutex multiple_wake_mutex;
boost::condition_variable multiple_wake_cond;
unsigned multiple_wake_count=0;
void wait_for_condvar_and_increase_count()
{
boost::mutex::scoped_lock lk(multiple_wake_mutex);
multiple_wake_cond.wait(lk);
++multiple_wake_count;
}
}
void do_test_notify_all_following_notify_one_wakes_all_threads()
{
boost::thread thread1(wait_for_condvar_and_increase_count);
boost::thread thread2(wait_for_condvar_and_increase_count);
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
multiple_wake_cond.notify_one();
boost::thread thread3(wait_for_condvar_and_increase_count);
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
multiple_wake_cond.notify_one();
multiple_wake_cond.notify_all();
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
{
boost::mutex::scoped_lock lk(multiple_wake_mutex);
BOOST_CHECK(multiple_wake_count==3);
}
thread1.join();
thread2.join();
thread3.join();
}
void test_condition_notify_all()
{
timed_test(&do_test_condition_notify_all_wakes_from_wait, timeout_seconds);
timed_test(&do_test_condition_notify_all_wakes_from_wait_with_predicate, timeout_seconds);
timed_test(&do_test_condition_notify_all_wakes_from_timed_wait, timeout_seconds);
timed_test(&do_test_condition_notify_all_wakes_from_timed_wait_with_predicate, timeout_seconds);
timed_test(&do_test_condition_notify_all_wakes_from_relative_timed_wait_with_predicate, timeout_seconds);
timed_test(&do_test_notify_all_following_notify_one_wakes_all_threads, timeout_seconds);
}
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_notify_all));
return test;
}