mirror of
https://github.com/boostorg/thread.git
synced 2026-02-09 11:32:12 +00:00
Compare commits
5 Commits
boost-1.28
...
svn-branch
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f25e80b47c | ||
|
|
43cbd3a283 | ||
|
|
31cf6b5e64 | ||
|
|
99109ab78b | ||
|
|
a80d5f159d |
@@ -38,7 +38,7 @@ if $(NT) && ! $(PTW32)
|
|||||||
{
|
{
|
||||||
dll boost_threadmon
|
dll boost_threadmon
|
||||||
: ../src/threadmon.cpp
|
: ../src/threadmon.cpp
|
||||||
: <include>$(BOOST_ROOT)
|
: <sysinclude>$(BOOST_ROOT)
|
||||||
<threading>multi
|
<threading>multi
|
||||||
: debug release <runtime-link>static/dynamic
|
: debug release <runtime-link>static/dynamic
|
||||||
;
|
;
|
||||||
@@ -53,7 +53,7 @@ CPP_SOURCES =
|
|||||||
|
|
||||||
lib boost_thread
|
lib boost_thread
|
||||||
: ../src/$(CPP_SOURCES).cpp
|
: ../src/$(CPP_SOURCES).cpp
|
||||||
: <include>$(BOOST_ROOT)
|
: <sysinclude>$(BOOST_ROOT)
|
||||||
<threading>multi
|
<threading>multi
|
||||||
$(pthreads-win32)
|
$(pthreads-win32)
|
||||||
: debug release <runtime-link>static/dynamic
|
: debug release <runtime-link>static/dynamic
|
||||||
@@ -62,9 +62,9 @@ lib boost_thread
|
|||||||
#######################
|
#######################
|
||||||
# Stage the generated targets.
|
# Stage the generated targets.
|
||||||
|
|
||||||
stage bin-stage
|
#stage bin-stage
|
||||||
: <lib>boost_thread $(threadmon)
|
# : <lib>boost_thread $(threadmon)
|
||||||
: <tag><runtime-link-static>"s"
|
# : <tag><runtime-link-static>"s"
|
||||||
<tag><debug>"d"
|
# <tag><debug>"d"
|
||||||
: debug release <runtime-link>static/dynamic
|
# : debug release <runtime-link>static/dynamic
|
||||||
;
|
#;
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ exe monitor
|
|||||||
: monitor/monitor.cpp
|
: monitor/monitor.cpp
|
||||||
<lib>../build/boost_thread
|
<lib>../build/boost_thread
|
||||||
$(threadmon)
|
$(threadmon)
|
||||||
: <include>$(BOOST_ROOT)
|
: <sysinclude>$(BOOST_ROOT)
|
||||||
$(pthreads-win32)
|
$(pthreads-win32)
|
||||||
<threading>multi
|
<threading>multi
|
||||||
: debug release <runtime-link>static/dynamic
|
: debug release <runtime-link>static/dynamic
|
||||||
@@ -48,7 +48,7 @@ exe starvephil
|
|||||||
: starvephil/starvephil.cpp
|
: starvephil/starvephil.cpp
|
||||||
<lib>../build/boost_thread
|
<lib>../build/boost_thread
|
||||||
$(threadmon)
|
$(threadmon)
|
||||||
: <include>$(BOOST_ROOT)
|
: <sysinclude>$(BOOST_ROOT)
|
||||||
$(pthreads-win32)
|
$(pthreads-win32)
|
||||||
<threading>multi
|
<threading>multi
|
||||||
: debug release <runtime-link>static/dynamic
|
: debug release <runtime-link>static/dynamic
|
||||||
@@ -61,7 +61,7 @@ exe tennis
|
|||||||
: tennis/tennis.cpp
|
: tennis/tennis.cpp
|
||||||
<lib>../build/boost_thread
|
<lib>../build/boost_thread
|
||||||
$(threadmon)
|
$(threadmon)
|
||||||
: <include>$(BOOST_ROOT)
|
: <sysinclude>$(BOOST_ROOT)
|
||||||
$(pthreads-win32)
|
$(pthreads-win32)
|
||||||
<threading>multi
|
<threading>multi
|
||||||
: debug release <runtime-link>static/dynamic
|
: debug release <runtime-link>static/dynamic
|
||||||
|
|||||||
@@ -40,6 +40,13 @@ struct xtime
|
|||||||
};
|
};
|
||||||
|
|
||||||
int xtime_get(struct xtime* xtp, int clock_type);
|
int xtime_get(struct xtime* xtp, int clock_type);
|
||||||
|
inline int xtime_cmp(const xtime& xt1, const xtime& xt2)
|
||||||
|
{
|
||||||
|
int res = (int)(xt1.sec - xt2.sec);
|
||||||
|
if (res == 0)
|
||||||
|
res = (int)(xt1.nsec - xt2.nsec);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
|
||||||
|
|||||||
@@ -244,7 +244,7 @@ void condition::do_wait()
|
|||||||
|
|
||||||
bool condition::do_timed_wait(const xtime& xt)
|
bool condition::do_timed_wait(const xtime& xt)
|
||||||
{
|
{
|
||||||
unsigned milliseconds;
|
int milliseconds;
|
||||||
to_duration(xt, milliseconds);
|
to_duration(xt, milliseconds);
|
||||||
|
|
||||||
unsigned int res = 0;
|
unsigned int res = 0;
|
||||||
@@ -571,7 +571,7 @@ void condition::do_wait()
|
|||||||
|
|
||||||
bool condition::do_timed_wait(const xtime& xt)
|
bool condition::do_timed_wait(const xtime& xt)
|
||||||
{
|
{
|
||||||
unsigned milliseconds;
|
int milliseconds;
|
||||||
to_duration(xt, milliseconds);
|
to_duration(xt, milliseconds);
|
||||||
|
|
||||||
OSStatus lStatus = noErr;
|
OSStatus lStatus = noErr;
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ bool timed_mutex::do_trylock()
|
|||||||
|
|
||||||
bool timed_mutex::do_timedlock(const xtime& xt)
|
bool timed_mutex::do_timedlock(const xtime& xt)
|
||||||
{
|
{
|
||||||
unsigned milliseconds;
|
int milliseconds;
|
||||||
to_duration(xt, milliseconds);
|
to_duration(xt, milliseconds);
|
||||||
|
|
||||||
unsigned int res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_mutex), milliseconds);
|
unsigned int res = WaitForSingleObject(reinterpret_cast<HANDLE>(m_mutex), milliseconds);
|
||||||
@@ -500,7 +500,7 @@ bool timed_mutex::do_trylock()
|
|||||||
|
|
||||||
bool timed_mutex::do_timedlock(const xtime& xt)
|
bool timed_mutex::do_timedlock(const xtime& xt)
|
||||||
{
|
{
|
||||||
unsigned microseconds;
|
int microseconds;
|
||||||
to_microduration(xt, microseconds);
|
to_microduration(xt, microseconds);
|
||||||
Duration lDuration = kDurationMicrosecond * microseconds;
|
Duration lDuration = kDurationMicrosecond * microseconds;
|
||||||
|
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ bool recursive_timed_mutex::do_trylock()
|
|||||||
|
|
||||||
bool recursive_timed_mutex::do_timedlock(const xtime& xt)
|
bool recursive_timed_mutex::do_timedlock(const xtime& xt)
|
||||||
{
|
{
|
||||||
unsigned milliseconds;
|
int milliseconds;
|
||||||
to_duration(xt, milliseconds);
|
to_duration(xt, milliseconds);
|
||||||
|
|
||||||
unsigned int res = 0;
|
unsigned int res = 0;
|
||||||
@@ -936,7 +936,7 @@ bool recursive_timed_mutex::do_trylock()
|
|||||||
|
|
||||||
bool recursive_timed_mutex::do_timedlock(const xtime& xt)
|
bool recursive_timed_mutex::do_timedlock(const xtime& xt)
|
||||||
{
|
{
|
||||||
unsigned microseconds;
|
int microseconds;
|
||||||
to_microduration(xt, microseconds);
|
to_microduration(xt, microseconds);
|
||||||
Duration lDuration = kDurationMicrosecond * microseconds;
|
Duration lDuration = kDurationMicrosecond * microseconds;
|
||||||
|
|
||||||
|
|||||||
@@ -191,14 +191,18 @@ void thread::join()
|
|||||||
|
|
||||||
void thread::sleep(const xtime& xt)
|
void thread::sleep(const xtime& xt)
|
||||||
{
|
{
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
#if defined(BOOST_HAS_WINTHREADS)
|
#if defined(BOOST_HAS_WINTHREADS)
|
||||||
unsigned milliseconds;
|
int milliseconds;
|
||||||
to_duration(xt, milliseconds);
|
to_duration(xt, milliseconds);
|
||||||
Sleep(milliseconds);
|
Sleep(milliseconds);
|
||||||
|
xtime xt2;
|
||||||
|
xtime_get(&xt2, TIME_UTC);
|
||||||
#elif defined(BOOST_HAS_PTHREADS)
|
#elif defined(BOOST_HAS_PTHREADS)
|
||||||
# if defined(BOOST_HAS_PTHREAD_DELAY_NP)
|
# if defined(BOOST_HAS_PTHREAD_DELAY_NP)
|
||||||
timespec ts;
|
timespec ts;
|
||||||
to_timespec(xt, ts);
|
to_timespec_duration(xt, ts);
|
||||||
int res = 0;
|
int res = 0;
|
||||||
res = pthread_delay_np(&ts);
|
res = pthread_delay_np(&ts);
|
||||||
assert(res == 0);
|
assert(res == 0);
|
||||||
@@ -216,12 +220,15 @@ void thread::sleep(const xtime& xt)
|
|||||||
cond.timed_wait(lock, xt);
|
cond.timed_wait(lock, xt);
|
||||||
# endif
|
# endif
|
||||||
#elif defined(BOOST_HAS_MPTASKS)
|
#elif defined(BOOST_HAS_MPTASKS)
|
||||||
unsigned microseconds;
|
int microseconds;
|
||||||
to_microduration(xt, microseconds);
|
to_microduration(xt, microseconds);
|
||||||
Duration lMicroseconds(kDurationMicrosecond * microseconds);
|
Duration lMicroseconds(kDurationMicrosecond * microseconds);
|
||||||
AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds));
|
AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds));
|
||||||
threads::mac::detail::safe_delay_until(&sWakeTime);
|
threads::mac::detail::safe_delay_until(&sWakeTime);
|
||||||
#endif
|
#endif
|
||||||
|
if (xtime_cmp(xt, xt2) >= 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread::yield()
|
void thread::yield()
|
||||||
|
|||||||
@@ -10,14 +10,14 @@
|
|||||||
// It is provided "as is" without express or implied warranty.
|
// It is provided "as is" without express or implied warranty.
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const unsigned MILLISECONDS_PER_SECOND = 1000;
|
const int MILLISECONDS_PER_SECOND = 1000;
|
||||||
const unsigned NANOSECONDS_PER_SECOND = 1000000000;
|
const int NANOSECONDS_PER_SECOND = 1000000000;
|
||||||
const unsigned NANOSECONDS_PER_MILLISECOND = 1000000;
|
const int NANOSECONDS_PER_MILLISECOND = 1000000;
|
||||||
|
|
||||||
const unsigned MICROSECONDS_PER_SECOND = 1000000;
|
const int MICROSECONDS_PER_SECOND = 1000000;
|
||||||
const unsigned NANOSECONDS_PER_MICROSECOND = 1000;
|
const int NANOSECONDS_PER_MICROSECOND = 1000;
|
||||||
|
|
||||||
inline void to_time(unsigned milliseconds, boost::xtime& xt)
|
inline void to_time(int milliseconds, boost::xtime& xt)
|
||||||
{
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
res = boost::xtime_get(&xt, boost::TIME_UTC);
|
res = boost::xtime_get(&xt, boost::TIME_UTC);
|
||||||
@@ -45,7 +45,7 @@ namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void to_time(unsigned milliseconds, timespec& ts)
|
inline void to_time(int milliseconds, timespec& ts)
|
||||||
{
|
{
|
||||||
boost::xtime xt;
|
boost::xtime xt;
|
||||||
to_time(milliseconds, xt);
|
to_time(milliseconds, xt);
|
||||||
@@ -83,7 +83,7 @@ namespace {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
inline void to_duration(const boost::xtime& xt, unsigned& milliseconds)
|
inline void to_duration(const boost::xtime& xt, int& milliseconds)
|
||||||
{
|
{
|
||||||
boost::xtime cur;
|
boost::xtime cur;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
@@ -94,13 +94,13 @@ namespace {
|
|||||||
milliseconds = 0;
|
milliseconds = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
milliseconds = static_cast<unsigned>(((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) +
|
milliseconds = ((xt.sec - cur.sec) * MILLISECONDS_PER_SECOND) +
|
||||||
(((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) /
|
(((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MILLISECOND/2)) /
|
||||||
NANOSECONDS_PER_MILLISECOND));
|
NANOSECONDS_PER_MILLISECOND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void to_microduration(const boost::xtime& xt, unsigned& microseconds)
|
inline void to_microduration(const boost::xtime& xt, int& microseconds)
|
||||||
{
|
{
|
||||||
boost::xtime cur;
|
boost::xtime cur;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
@@ -111,9 +111,9 @@ namespace {
|
|||||||
microseconds = 0;
|
microseconds = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
microseconds = static_cast<unsigned long>(((xt.sec - cur.sec) * MICROSECONDS_PER_SECOND) +
|
microseconds = ((xt.sec - cur.sec) * MICROSECONDS_PER_SECOND) +
|
||||||
(((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MICROSECOND/2)) /
|
(((xt.nsec - cur.nsec) + (NANOSECONDS_PER_MICROSECOND/2)) /
|
||||||
NANOSECONDS_PER_MICROSECOND));
|
NANOSECONDS_PER_MICROSECOND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,14 +26,17 @@ subproject libs/thread/test ;
|
|||||||
SEARCH on <module@>threads.jam = $(BOOST_ROOT)/libs/thread/build ;
|
SEARCH on <module@>threads.jam = $(BOOST_ROOT)/libs/thread/build ;
|
||||||
include <module@>threads.jam ;
|
include <module@>threads.jam ;
|
||||||
|
|
||||||
|
sources = test.cpp test_thread.cpp test_mutex.cpp test_condition.cpp test_tss.cpp test_once.cpp ;
|
||||||
|
|
||||||
#######################
|
#######################
|
||||||
# Declare the Boost.Threads unit test program test_thread.
|
# Declare the Boost.Threads unit test program test_thread.
|
||||||
|
|
||||||
unit-test test_thread
|
unit-test test_thread
|
||||||
: test_thread.cpp
|
: $(sources)
|
||||||
<lib>../build/boost_thread
|
<lib>../build/boost_thread
|
||||||
|
<lib>../../test/build/unit_test_framework
|
||||||
$(threadmon)
|
$(threadmon)
|
||||||
: <include>$(BOOST_ROOT)
|
: <sysinclude>$(BOOST_ROOT)
|
||||||
$(pthreads-win32)
|
$(pthreads-win32)
|
||||||
<threading>multi
|
<threading>multi
|
||||||
: debug release <runtime-link>static/dynamic
|
: debug release <runtime-link>static/dynamic
|
||||||
|
|||||||
20
test/test.cpp
Normal file
20
test/test.cpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
extern boost::unit_test_framework::test_suite* thread_tests();
|
||||||
|
extern boost::unit_test_framework::test_suite* mutex_tests();
|
||||||
|
extern boost::unit_test_framework::test_suite* condition_tests();
|
||||||
|
extern boost::unit_test_framework::test_suite* tss_tests();
|
||||||
|
extern boost::unit_test_framework::test_suite* once_tests();
|
||||||
|
|
||||||
|
boost::unit_test_framework::test_suite* init_unit_test_suite(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads test suite");
|
||||||
|
|
||||||
|
test->add(thread_tests());
|
||||||
|
test->add(mutex_tests());
|
||||||
|
test->add(condition_tests());
|
||||||
|
test->add(tss_tests());
|
||||||
|
test->add(once_tests());
|
||||||
|
|
||||||
|
return test;
|
||||||
|
}
|
||||||
191
test/test_condition.cpp
Normal file
191
test/test_condition.cpp
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
#include <boost/thread/condition.hpp>
|
||||||
|
#include <boost/thread/thread.hpp>
|
||||||
|
#include <boost/thread/xtime.hpp>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct condition_test_data
|
||||||
|
{
|
||||||
|
condition_test_data() : notified(0), awoken(0) { }
|
||||||
|
|
||||||
|
boost::mutex mutex;
|
||||||
|
boost::condition condition;
|
||||||
|
int notified;
|
||||||
|
int awoken;
|
||||||
|
};
|
||||||
|
|
||||||
|
void condition_test_thread(void* param)
|
||||||
|
{
|
||||||
|
condition_test_data* data = static_cast<condition_test_data*>(param);
|
||||||
|
boost::mutex::scoped_lock lock(data->mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
while (!(data->notified > 0))
|
||||||
|
data->condition.wait(lock);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
data->awoken++;
|
||||||
|
}
|
||||||
|
|
||||||
|
class thread_adapter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
|
||||||
|
void operator()() const { _func(_param); }
|
||||||
|
private:
|
||||||
|
void (*_func)(void*);
|
||||||
|
void* _param;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct cond_predicate
|
||||||
|
{
|
||||||
|
cond_predicate(int& var, int val) : _var(var), _val(val) { }
|
||||||
|
|
||||||
|
bool operator()() { return _var == _val; }
|
||||||
|
|
||||||
|
int& _var;
|
||||||
|
int _val;
|
||||||
|
};
|
||||||
|
|
||||||
|
void condition_test_waits(void* param)
|
||||||
|
{
|
||||||
|
condition_test_data* data = static_cast<condition_test_data*>(param);
|
||||||
|
|
||||||
|
boost::mutex::scoped_lock lock(data->mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Test wait.
|
||||||
|
while (data->notified != 1)
|
||||||
|
data->condition.wait(lock);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
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);
|
||||||
|
BOOST_CHECK_EQUAL(data->notified, 2);
|
||||||
|
data->awoken++;
|
||||||
|
data->condition.notify_one();
|
||||||
|
|
||||||
|
// Test timed_wait.
|
||||||
|
boost::xtime xt;
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.nsec += 100000000;
|
||||||
|
while (data->notified != 3)
|
||||||
|
data->condition.timed_wait(lock, xt);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
BOOST_CHECK_EQUAL(data->notified, 3);
|
||||||
|
data->awoken++;
|
||||||
|
data->condition.notify_one();
|
||||||
|
|
||||||
|
// Test predicate timed_wait.
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.sec += 2;
|
||||||
|
BOOST_CHECK(data->condition.timed_wait(lock, xt, cond_predicate(data->notified, 4)));
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
BOOST_CHECK_EQUAL(data->notified, 4);
|
||||||
|
data->awoken++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_condition_notify_one()
|
||||||
|
{
|
||||||
|
condition_test_data data;
|
||||||
|
|
||||||
|
boost::thread thread(thread_adapter(&condition_test_thread, &data));
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(data.mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
data.notified++;
|
||||||
|
data.condition.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
thread.join();
|
||||||
|
BOOST_CHECK_EQUAL(data.awoken, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_condition_notify_all()
|
||||||
|
{
|
||||||
|
const int NUMTHREADS = 5;
|
||||||
|
boost::thread_group threads;
|
||||||
|
condition_test_data data;
|
||||||
|
|
||||||
|
for (int i = 0; i < NUMTHREADS; ++i)
|
||||||
|
threads.create_thread(thread_adapter(&condition_test_thread, &data));
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(data.mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
data.notified++;
|
||||||
|
data.condition.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
threads.join_all();
|
||||||
|
BOOST_CHECK_EQUAL(data.awoken, NUMTHREADS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_condition_waits()
|
||||||
|
{
|
||||||
|
condition_test_data data;
|
||||||
|
|
||||||
|
boost::thread thread(thread_adapter(&condition_test_waits, &data));
|
||||||
|
|
||||||
|
boost::xtime xt;
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(data.mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.sec += 1;
|
||||||
|
boost::thread::sleep(xt);
|
||||||
|
data.notified++;
|
||||||
|
data.condition.notify_one();
|
||||||
|
while (data.awoken != 1)
|
||||||
|
data.condition.wait(lock);
|
||||||
|
BOOST_CHECK_EQUAL(data.awoken, 1);
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.sec += 1;
|
||||||
|
boost::thread::sleep(xt);
|
||||||
|
data.notified++;
|
||||||
|
data.condition.notify_one();
|
||||||
|
while (data.awoken != 2)
|
||||||
|
data.condition.wait(lock);
|
||||||
|
BOOST_CHECK_EQUAL(data.awoken, 2);
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.sec += 1;
|
||||||
|
boost::thread::sleep(xt);
|
||||||
|
data.notified++;
|
||||||
|
data.condition.notify_one();
|
||||||
|
while (data.awoken != 3)
|
||||||
|
data.condition.wait(lock);
|
||||||
|
BOOST_CHECK_EQUAL(data.awoken, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.sec += 1;
|
||||||
|
boost::thread::sleep(xt);
|
||||||
|
data.notified++;
|
||||||
|
data.condition.notify_one();
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.sec += 1;
|
||||||
|
boost::thread::sleep(xt);
|
||||||
|
thread.join();
|
||||||
|
BOOST_CHECK_EQUAL(data.awoken, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::unit_test_framework::test_suite* condition_tests()
|
||||||
|
{
|
||||||
|
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: condition test suite");
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(&test_condition_notify_one));
|
||||||
|
test->add(BOOST_TEST_CASE(&test_condition_notify_all));
|
||||||
|
test->add(BOOST_TEST_CASE(&test_condition_waits));
|
||||||
|
|
||||||
|
return test;
|
||||||
|
}
|
||||||
184
test/test_mutex.cpp
Normal file
184
test/test_mutex.cpp
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
#include <boost/thread/mutex.hpp>
|
||||||
|
#include <boost/thread/recursive_mutex.hpp>
|
||||||
|
#include <boost/thread/xtime.hpp>
|
||||||
|
#include <boost/thread/condition.hpp>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <boost/test/unit_test_suite_ex.hpp>
|
||||||
|
|
||||||
|
template <typename M>
|
||||||
|
struct test_lock
|
||||||
|
{
|
||||||
|
typedef M mutex_type;
|
||||||
|
typedef typename M::scoped_lock lock_type;
|
||||||
|
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
mutex_type mutex;
|
||||||
|
boost::condition condition;
|
||||||
|
|
||||||
|
// Test the lock's constructors.
|
||||||
|
{
|
||||||
|
lock_type lock(mutex, false);
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
}
|
||||||
|
lock_type lock(mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Construct and initialize an xtime for a fast time out.
|
||||||
|
boost::xtime xt;
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.nsec += 100000000;
|
||||||
|
|
||||||
|
// Test the lock and the mutex with condition variables.
|
||||||
|
// No one is going to notify this condition variable. We expect to
|
||||||
|
// time out.
|
||||||
|
BOOST_CHECK(!condition.timed_wait(lock, xt));
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Test the lock and unlock methods.
|
||||||
|
lock.unlock();
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
lock.lock();
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename M>
|
||||||
|
struct test_trylock
|
||||||
|
{
|
||||||
|
typedef M mutex_type;
|
||||||
|
typedef typename M::scoped_try_lock try_lock_type;
|
||||||
|
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
mutex_type mutex;
|
||||||
|
boost::condition condition;
|
||||||
|
|
||||||
|
// Test the lock's constructors.
|
||||||
|
{
|
||||||
|
try_lock_type lock(mutex);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
try_lock_type lock(mutex, false);
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
}
|
||||||
|
try_lock_type lock(mutex, true);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Construct and initialize an xtime for a fast time out.
|
||||||
|
boost::xtime xt;
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.nsec += 100000000;
|
||||||
|
|
||||||
|
// Test the lock and the mutex with condition variables.
|
||||||
|
// No one is going to notify this condition variable. We expect to
|
||||||
|
// time out.
|
||||||
|
BOOST_CHECK(!condition.timed_wait(lock, xt));
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Test the lock, unlock and trylock methods.
|
||||||
|
lock.unlock();
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
lock.lock();
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
lock.unlock();
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
BOOST_CHECK(lock.try_lock());
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename M>
|
||||||
|
struct test_timedlock
|
||||||
|
{
|
||||||
|
typedef M mutex_type;
|
||||||
|
typedef typename M::scoped_timed_lock timed_lock_type;
|
||||||
|
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
mutex_type mutex;
|
||||||
|
boost::condition condition;
|
||||||
|
|
||||||
|
// Test the lock's constructors.
|
||||||
|
{
|
||||||
|
// Construct and initialize an xtime for a fast time out.
|
||||||
|
boost::xtime xt;
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.nsec += 100000000;
|
||||||
|
|
||||||
|
timed_lock_type lock(mutex, xt);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
timed_lock_type lock(mutex, false);
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
}
|
||||||
|
timed_lock_type lock(mutex, true);
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Construct and initialize an xtime for a fast time out.
|
||||||
|
boost::xtime xt;
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.nsec += 100000000;
|
||||||
|
|
||||||
|
// Test the lock and the mutex with condition variables.
|
||||||
|
// No one is going to notify this condition variable. We expect to
|
||||||
|
// time out.
|
||||||
|
BOOST_CHECK(!condition.timed_wait(lock, xt));
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
|
||||||
|
// Test the lock, unlock and timedlock methods.
|
||||||
|
lock.unlock();
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
lock.lock();
|
||||||
|
BOOST_CHECK(lock);
|
||||||
|
lock.unlock();
|
||||||
|
BOOST_CHECK(!lock);
|
||||||
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
|
xt.nsec += 100000000;
|
||||||
|
BOOST_CHECK(lock.timed_lock(xt));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename M>
|
||||||
|
struct test_recursive_lock
|
||||||
|
{
|
||||||
|
typedef M mutex;
|
||||||
|
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
mutex mx;
|
||||||
|
mutex::scoped_lock lock1(mx);
|
||||||
|
mutex::scoped_lock lock2(mx);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
boost::unit_test_framework::test_suite* mutex_tests()
|
||||||
|
{
|
||||||
|
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: mutex test suite");
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(test_lock<boost::mutex>()));
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(test_lock<boost::try_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_trylock<boost::try_mutex>()));
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(test_lock<boost::timed_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_trylock<boost::timed_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_timedlock<boost::timed_mutex>()));
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(test_lock<boost::recursive_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_recursive_lock<boost::recursive_mutex>()));
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(test_lock<boost::recursive_try_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_trylock<boost::recursive_try_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_recursive_lock<boost::recursive_try_mutex>()));
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(test_lock<boost::recursive_timed_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_trylock<boost::recursive_timed_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_timedlock<boost::recursive_timed_mutex>()));
|
||||||
|
test->add(BOOST_TEST_CASE(test_recursive_lock<boost::recursive_timed_mutex>()));
|
||||||
|
|
||||||
|
return test;
|
||||||
|
}
|
||||||
39
test/test_once.cpp
Normal file
39
test/test_once.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include <boost/thread/once.hpp>
|
||||||
|
#include <boost/thread/thread.hpp>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
int once_value = 0;
|
||||||
|
boost::once_flag once = BOOST_ONCE_INIT;
|
||||||
|
|
||||||
|
void init_once_value()
|
||||||
|
{
|
||||||
|
once_value++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_once_thread()
|
||||||
|
{
|
||||||
|
boost::call_once(&init_once_value, once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_once()
|
||||||
|
{
|
||||||
|
const int NUMTHREADS=5;
|
||||||
|
boost::thread_group threads;
|
||||||
|
for (int i=0; i<NUMTHREADS; ++i)
|
||||||
|
threads.create_thread(&test_once_thread);
|
||||||
|
threads.join_all();
|
||||||
|
BOOST_CHECK_EQUAL(once_value, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::unit_test_framework::test_suite* once_tests()
|
||||||
|
{
|
||||||
|
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: once test suite");
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(&test_once));
|
||||||
|
|
||||||
|
return test;
|
||||||
|
}
|
||||||
@@ -1,451 +1,89 @@
|
|||||||
#include <list>
|
|
||||||
#include <boost/thread/mutex.hpp>
|
|
||||||
#include <boost/thread/recursive_mutex.hpp>
|
|
||||||
#include <boost/thread/condition.hpp>
|
|
||||||
#include <boost/thread/tss.hpp>
|
|
||||||
#include <boost/thread/once.hpp>
|
|
||||||
#include <boost/thread/thread.hpp>
|
#include <boost/thread/thread.hpp>
|
||||||
#include <boost/thread/xtime.hpp>
|
#include <boost/thread/xtime.hpp>
|
||||||
|
|
||||||
#define BOOST_INCLUDE_MAIN
|
#include <boost/test/unit_test.hpp>
|
||||||
#include <boost/test/test_tools.hpp>
|
|
||||||
|
|
||||||
#if defined(BOOST_HAS_WINTHREADS)
|
namespace
|
||||||
# include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template <typename M>
|
|
||||||
void test_lock(M* dummy=0)
|
|
||||||
{
|
{
|
||||||
typedef M mutex_type;
|
inline bool xtime_in_range(boost::xtime& xt, int less_seconds, int greater_seconds)
|
||||||
typedef typename M::scoped_lock lock_type;
|
|
||||||
|
|
||||||
mutex_type mutex;
|
|
||||||
boost::condition condition;
|
|
||||||
|
|
||||||
// Test the lock's constructors.
|
|
||||||
{
|
{
|
||||||
lock_type lock(mutex, false);
|
boost::xtime cur;
|
||||||
BOOST_TEST(!lock);
|
BOOST_CHECK_EQUAL(boost::xtime_get(&cur, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
}
|
|
||||||
lock_type lock(mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Construct and initialize an xtime for a fast time out.
|
boost::xtime less = cur;
|
||||||
|
less.sec += less_seconds;
|
||||||
|
|
||||||
|
boost::xtime greater = cur;
|
||||||
|
greater.sec += greater_seconds;
|
||||||
|
|
||||||
|
return (boost::xtime_cmp(xt, less) >= 0) && (boost::xtime_cmp(xt, greater) <= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_value;
|
||||||
|
|
||||||
|
void simple_thread()
|
||||||
|
{
|
||||||
|
test_value = 999;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct thread_adapter
|
||||||
|
{
|
||||||
|
thread_adapter(void (*func)(boost::thread& parent), boost::thread& parent)
|
||||||
|
: func(func), parent(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
(*func)(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void (*func)(boost::thread& parent);
|
||||||
|
boost::thread& parent;
|
||||||
|
};
|
||||||
|
|
||||||
|
void comparison_thread(boost::thread& parent)
|
||||||
|
{
|
||||||
|
boost::thread thrd;
|
||||||
|
BOOST_TEST(thrd != parent);
|
||||||
|
BOOST_TEST(thrd == boost::thread());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_sleep()
|
||||||
|
{
|
||||||
boost::xtime xt;
|
boost::xtime xt;
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
BOOST_CHECK_EQUAL(boost::xtime_get(&xt, boost::TIME_UTC), boost::TIME_UTC);
|
||||||
xt.nsec += 100000000;
|
xt.sec += 5;
|
||||||
|
|
||||||
// Test the lock and the mutex with condition variables.
|
|
||||||
// No one is going to notify this condition variable. We expect to
|
|
||||||
// time out.
|
|
||||||
BOOST_TEST(condition.timed_wait(lock, xt) == false);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Test the lock and unlock methods.
|
|
||||||
lock.unlock();
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
lock.lock();
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename M>
|
|
||||||
void test_trylock(M* dummy=0)
|
|
||||||
{
|
|
||||||
typedef M mutex_type;
|
|
||||||
typedef typename M::scoped_try_lock try_lock_type;
|
|
||||||
|
|
||||||
mutex_type mutex;
|
|
||||||
boost::condition condition;
|
|
||||||
|
|
||||||
// Test the lock's constructors.
|
|
||||||
{
|
|
||||||
try_lock_type lock(mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
try_lock_type lock(mutex, false);
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
}
|
|
||||||
try_lock_type lock(mutex, true);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Construct and initialize an xtime for a fast time out.
|
|
||||||
boost::xtime xt;
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.nsec += 100000000;
|
|
||||||
|
|
||||||
// Test the lock and the mutex with condition variables.
|
|
||||||
// No one is going to notify this condition variable. We expect to
|
|
||||||
// time out.
|
|
||||||
BOOST_TEST(condition.timed_wait(lock, xt) == false);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Test the lock, unlock and trylock methods.
|
|
||||||
lock.unlock();
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
lock.lock();
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
lock.unlock();
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
BOOST_TEST(lock.try_lock());
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename M>
|
|
||||||
void test_timedlock(M* dummy=0)
|
|
||||||
{
|
|
||||||
typedef M mutex_type;
|
|
||||||
typedef typename M::scoped_timed_lock timed_lock_type;
|
|
||||||
|
|
||||||
mutex_type mutex;
|
|
||||||
boost::condition condition;
|
|
||||||
|
|
||||||
// Test the lock's constructors.
|
|
||||||
{
|
|
||||||
// Construct and initialize an xtime for a fast time out.
|
|
||||||
boost::xtime xt;
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.nsec += 100000000;
|
|
||||||
|
|
||||||
timed_lock_type lock(mutex, xt);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
timed_lock_type lock(mutex, false);
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
}
|
|
||||||
timed_lock_type lock(mutex, true);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Construct and initialize an xtime for a fast time out.
|
|
||||||
boost::xtime xt;
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.nsec += 100000000;
|
|
||||||
|
|
||||||
// Test the lock and the mutex with condition variables.
|
|
||||||
// No one is going to notify this condition variable. We expect to
|
|
||||||
// time out.
|
|
||||||
BOOST_TEST(condition.timed_wait(lock, xt) == false);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Test the lock, unlock and timedlock methods.
|
|
||||||
lock.unlock();
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
lock.lock();
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
lock.unlock();
|
|
||||||
BOOST_TEST(!lock);
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.nsec += 100000000;
|
|
||||||
BOOST_TEST(lock.timed_lock(xt));
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_mutex()
|
|
||||||
{
|
|
||||||
typedef boost::mutex mutex;
|
|
||||||
test_lock<mutex>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_try_mutex()
|
|
||||||
{
|
|
||||||
typedef boost::try_mutex mutex;
|
|
||||||
test_lock<mutex>();
|
|
||||||
test_trylock<mutex>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_timed_mutex()
|
|
||||||
{
|
|
||||||
typedef boost::timed_mutex mutex;
|
|
||||||
test_lock<mutex>();
|
|
||||||
test_trylock<mutex>();
|
|
||||||
test_timedlock<mutex>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_recursive_mutex()
|
|
||||||
{
|
|
||||||
typedef boost::recursive_mutex mutex;
|
|
||||||
test_lock<mutex>();
|
|
||||||
mutex mx;
|
|
||||||
mutex::scoped_lock lock1(mx);
|
|
||||||
mutex::scoped_lock lock2(mx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_recursive_try_mutex()
|
|
||||||
{
|
|
||||||
typedef boost::recursive_try_mutex mutex;
|
|
||||||
test_lock<mutex>();
|
|
||||||
test_trylock<mutex>();
|
|
||||||
mutex mx;
|
|
||||||
mutex::scoped_lock lock1(mx);
|
|
||||||
mutex::scoped_lock lock2(mx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_recursive_timed_mutex()
|
|
||||||
{
|
|
||||||
typedef boost::recursive_timed_mutex mutex;
|
|
||||||
test_lock<mutex>();
|
|
||||||
test_trylock<mutex>();
|
|
||||||
test_timedlock<mutex>();
|
|
||||||
mutex mx;
|
|
||||||
mutex::scoped_lock lock1(mx);
|
|
||||||
mutex::scoped_lock lock2(mx);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct condition_test_data
|
|
||||||
{
|
|
||||||
condition_test_data() : notified(0), awoken(0) { }
|
|
||||||
|
|
||||||
boost::mutex mutex;
|
|
||||||
boost::condition condition;
|
|
||||||
int notified;
|
|
||||||
int awoken;
|
|
||||||
};
|
|
||||||
|
|
||||||
void condition_test_thread(void* param)
|
|
||||||
{
|
|
||||||
condition_test_data* data = static_cast<condition_test_data*>(param);
|
|
||||||
boost::mutex::scoped_lock lock(data->mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
while (!(data->notified > 0))
|
|
||||||
data->condition.wait(lock);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
data->awoken++;
|
|
||||||
}
|
|
||||||
|
|
||||||
class thread_adapter
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
|
|
||||||
void operator()() const { _func(_param); }
|
|
||||||
private:
|
|
||||||
void (*_func)(void*);
|
|
||||||
void* _param;
|
|
||||||
};
|
|
||||||
|
|
||||||
void test_condition_notify_one()
|
|
||||||
{
|
|
||||||
condition_test_data data;
|
|
||||||
|
|
||||||
boost::thread thread(thread_adapter(&condition_test_thread, &data));
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::mutex::scoped_lock lock(data.mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
data.notified++;
|
|
||||||
data.condition.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
thread.join();
|
|
||||||
BOOST_TEST(data.awoken == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_condition_notify_all()
|
|
||||||
{
|
|
||||||
const int NUMTHREADS = 5;
|
|
||||||
boost::thread_group threads;
|
|
||||||
condition_test_data data;
|
|
||||||
|
|
||||||
for (int i = 0; i < NUMTHREADS; ++i)
|
|
||||||
threads.create_thread(thread_adapter(&condition_test_thread, &data));
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::mutex::scoped_lock lock(data.mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
data.notified++;
|
|
||||||
data.condition.notify_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
threads.join_all();
|
|
||||||
BOOST_TEST(data.awoken == NUMTHREADS);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct cond_predicate
|
|
||||||
{
|
|
||||||
cond_predicate(int& var, int val) : _var(var), _val(val) { }
|
|
||||||
|
|
||||||
bool operator()() { return _var == _val; }
|
|
||||||
|
|
||||||
int& _var;
|
|
||||||
int _val;
|
|
||||||
};
|
|
||||||
|
|
||||||
void condition_test_waits(void* param)
|
|
||||||
{
|
|
||||||
condition_test_data* data = static_cast<condition_test_data*>(param);
|
|
||||||
|
|
||||||
boost::mutex::scoped_lock lock(data->mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
// Test wait.
|
|
||||||
while (data->notified != 1)
|
|
||||||
data->condition.wait(lock);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
BOOST_TEST(data->notified == 1);
|
|
||||||
data->awoken++;
|
|
||||||
data->condition.notify_one();
|
|
||||||
|
|
||||||
// Test predicate wait.
|
|
||||||
data->condition.wait(lock, cond_predicate(data->notified, 2));
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
BOOST_TEST(data->notified == 2);
|
|
||||||
data->awoken++;
|
|
||||||
data->condition.notify_one();
|
|
||||||
|
|
||||||
// Test timed_wait.
|
|
||||||
boost::xtime xt;
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.nsec += 100000000;
|
|
||||||
while (data->notified != 3)
|
|
||||||
data->condition.timed_wait(lock, xt);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
BOOST_TEST(data->notified == 3);
|
|
||||||
data->awoken++;
|
|
||||||
data->condition.notify_one();
|
|
||||||
|
|
||||||
// Test predicate timed_wait.
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.sec += 2;
|
|
||||||
BOOST_TEST(data->condition.timed_wait(lock, xt, cond_predicate(data->notified, 4)));
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
BOOST_TEST(data->notified == 4);
|
|
||||||
data->awoken++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_condition_waits()
|
|
||||||
{
|
|
||||||
condition_test_data data;
|
|
||||||
|
|
||||||
boost::thread thread(thread_adapter(&condition_test_waits, &data));
|
|
||||||
|
|
||||||
boost::xtime xt;
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::mutex::scoped_lock lock(data.mutex);
|
|
||||||
BOOST_TEST(lock);
|
|
||||||
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.sec += 1;
|
|
||||||
boost::thread::sleep(xt);
|
boost::thread::sleep(xt);
|
||||||
data.notified++;
|
|
||||||
data.condition.notify_one();
|
|
||||||
while (data.awoken != 1)
|
|
||||||
data.condition.wait(lock);
|
|
||||||
BOOST_TEST(data.awoken == 1);
|
|
||||||
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
// Insure it's in a range instead of checking actual equality due to time lapse
|
||||||
xt.sec += 1;
|
BOOST_CHECK(xtime_in_range(xt, -1, 1));
|
||||||
boost::thread::sleep(xt);
|
|
||||||
data.notified++;
|
|
||||||
data.condition.notify_one();
|
|
||||||
while (data.awoken != 2)
|
|
||||||
data.condition.wait(lock);
|
|
||||||
BOOST_TEST(data.awoken == 2);
|
|
||||||
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.sec += 1;
|
|
||||||
boost::thread::sleep(xt);
|
|
||||||
data.notified++;
|
|
||||||
data.condition.notify_one();
|
|
||||||
while (data.awoken != 3)
|
|
||||||
data.condition.wait(lock);
|
|
||||||
BOOST_TEST(data.awoken == 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.sec += 1;
|
|
||||||
boost::thread::sleep(xt);
|
|
||||||
data.notified++;
|
|
||||||
data.condition.notify_one();
|
|
||||||
BOOST_TEST(boost::xtime_get(&xt, boost::TIME_UTC) == boost::TIME_UTC);
|
|
||||||
xt.sec += 1;
|
|
||||||
boost::thread::sleep(xt);
|
|
||||||
thread.join();
|
|
||||||
BOOST_TEST(data.awoken == 4);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_condition()
|
void test_creation()
|
||||||
{
|
{
|
||||||
test_condition_notify_one();
|
test_value = 0;
|
||||||
test_condition_notify_all();
|
boost::thread thrd(&simple_thread);
|
||||||
test_condition_waits();
|
thrd.join();
|
||||||
|
BOOST_CHECK_EQUAL(test_value, 999);
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::mutex tss_mutex;
|
void test_comparison()
|
||||||
int tss_instances = 0;
|
|
||||||
|
|
||||||
struct tss_value_t
|
|
||||||
{
|
{
|
||||||
tss_value_t()
|
boost::thread self;
|
||||||
{
|
boost::thread thrd(thread_adapter(&comparison_thread, self));
|
||||||
boost::mutex::scoped_lock lock(tss_mutex);
|
thrd.join();
|
||||||
++tss_instances;
|
|
||||||
value = 0;
|
|
||||||
}
|
|
||||||
~tss_value_t()
|
|
||||||
{
|
|
||||||
boost::mutex::scoped_lock lock(tss_mutex);
|
|
||||||
--tss_instances;
|
|
||||||
}
|
|
||||||
int value;
|
|
||||||
};
|
|
||||||
|
|
||||||
boost::thread_specific_ptr<tss_value_t> tss_value;
|
|
||||||
|
|
||||||
void test_tss_thread()
|
|
||||||
{
|
|
||||||
tss_value.reset(new tss_value_t());
|
|
||||||
for (int i=0; i<1000; ++i)
|
|
||||||
{
|
|
||||||
int& n = tss_value->value;
|
|
||||||
BOOST_TEST(n == i);
|
|
||||||
++n;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_tss()
|
boost::unit_test_framework::test_suite* thread_tests()
|
||||||
{
|
{
|
||||||
const int NUMTHREADS=5;
|
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: thread test suite");
|
||||||
boost::thread_group threads;
|
|
||||||
for (int i=0; i<NUMTHREADS; ++i)
|
|
||||||
threads.create_thread(&test_tss_thread);
|
|
||||||
threads.join_all();
|
|
||||||
BOOST_TEST(tss_instances == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int once_value = 0;
|
test->add(BOOST_TEST_CASE(&test_sleep));
|
||||||
boost::once_flag once = BOOST_ONCE_INIT;
|
test->add(BOOST_TEST_CASE(&test_creation));
|
||||||
|
test->add(BOOST_TEST_CASE(&test_comparison));
|
||||||
|
|
||||||
void init_once_value()
|
return test;
|
||||||
{
|
|
||||||
once_value++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_once_thread()
|
|
||||||
{
|
|
||||||
boost::call_once(&init_once_value, once);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_once()
|
|
||||||
{
|
|
||||||
const int NUMTHREADS=5;
|
|
||||||
boost::thread_group threads;
|
|
||||||
for (int i=0; i<NUMTHREADS; ++i)
|
|
||||||
threads.create_thread(&test_once_thread);
|
|
||||||
threads.join_all();
|
|
||||||
BOOST_TEST(once_value == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int test_main(int, char*[])
|
|
||||||
{
|
|
||||||
test_mutex();
|
|
||||||
test_try_mutex();
|
|
||||||
test_timed_mutex();
|
|
||||||
test_recursive_mutex();
|
|
||||||
test_recursive_try_mutex();
|
|
||||||
test_recursive_timed_mutex();
|
|
||||||
test_condition();
|
|
||||||
test_tss();
|
|
||||||
test_once();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
59
test/test_tss.cpp
Normal file
59
test/test_tss.cpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#include <boost/thread/tss.hpp>
|
||||||
|
#include <boost/thread/mutex.hpp>
|
||||||
|
#include <boost/thread/thread.hpp>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
boost::mutex tss_mutex;
|
||||||
|
int tss_instances = 0;
|
||||||
|
|
||||||
|
struct tss_value_t
|
||||||
|
{
|
||||||
|
tss_value_t()
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(tss_mutex);
|
||||||
|
++tss_instances;
|
||||||
|
value = 0;
|
||||||
|
}
|
||||||
|
~tss_value_t()
|
||||||
|
{
|
||||||
|
boost::mutex::scoped_lock lock(tss_mutex);
|
||||||
|
--tss_instances;
|
||||||
|
}
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
boost::thread_specific_ptr<tss_value_t> tss_value;
|
||||||
|
|
||||||
|
void test_tss_thread()
|
||||||
|
{
|
||||||
|
tss_value.reset(new tss_value_t());
|
||||||
|
for (int i=0; i<1000; ++i)
|
||||||
|
{
|
||||||
|
int& n = tss_value->value;
|
||||||
|
BOOST_CHECK_EQUAL(n, i);
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_tss()
|
||||||
|
{
|
||||||
|
const int NUMTHREADS=5;
|
||||||
|
boost::thread_group threads;
|
||||||
|
for (int i=0; i<NUMTHREADS; ++i)
|
||||||
|
threads.create_thread(&test_tss_thread);
|
||||||
|
threads.join_all();
|
||||||
|
BOOST_CHECK_EQUAL(tss_instances, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::unit_test_framework::test_suite* tss_tests()
|
||||||
|
{
|
||||||
|
boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: tss test suite");
|
||||||
|
|
||||||
|
test->add(BOOST_TEST_CASE(&test_tss));
|
||||||
|
|
||||||
|
return test;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user