1 #define BOOST_TEST_MAIN
2 #include <boost/test/included/unit_test.hpp>
4 #include "semaphore.hpp"
6 #include <boost/thread/thread.hpp>
11 inline timespec
ptime_to_timespec (const boost::posix_time::ptime
&tm
)
13 const boost::posix_time::ptime
epoch(boost::gregorian::date(1970,1,1));
14 boost::posix_time::time_duration
duration (tm
- epoch
);
16 ts
.tv_sec
= duration
.total_seconds();
17 ts
.tv_nsec
= duration
.total_nanoseconds() % 1000000000;
22 BOOST_AUTO_TEST_CASE( sem_timed_wait
)
26 system_time
const timeout
= get_system_time() + posix_time::milliseconds(500);
28 struct timespec timeoutspec
= ptime_to_timespec(timeout
);
29 int status
= sem
.timed_wait(timeoutspec
);
30 BOOST_REQUIRE(!status
);
36 const int thread_count
= 8;
37 const int iterations_per_thread
= 100000;
45 for (int i
= 0; i
!= iterations_per_thread
; ++i
) {
54 BOOST_AUTO_TEST_CASE( sem_test
)
58 for (int i
= 0; i
!= thread_count
; ++i
)
59 g
.create_thread(test_fn
);
62 BOOST_REQUIRE_EQUAL(count
, iterations_per_thread
* thread_count
);
65 BOOST_AUTO_TEST_CASE( sem_sync_test
)
69 semaphore_sync
<semaphore
> sync(sem
);