@@ -24,63 +24,58 @@ using namespace boost::chrono;
2424
2525typedef boost::concurrent::sync_timed_queue<int > sync_tq;
2626
27- const int count = 5 ;
27+ const int cnt = 5 ;
2828
29- void call_push (sync_tq* q)
29+ void call_push (sync_tq* q, const steady_clock::time_point start )
3030{
3131 // push elements onto the queue every 500 milliseconds but with a decreasing delay each time
32- for (int i = 0 ; i < count ; ++i)
32+ for (int i = 0 ; i < cnt ; ++i)
3333 {
34- q->push (i, sync_tq::clock::now () + seconds (count - i));
35- boost::this_thread::sleep_for (milliseconds (500 ));
34+ boost::this_thread::sleep_until (start + milliseconds (i * 500 ));
35+ const steady_clock::time_point expected = start + milliseconds (i * 500 ) + seconds (cnt - i);
36+ q->push (i, expected);
3637 }
3738}
3839
39- void call_pull (sync_tq* q)
40+ void call_pull (sync_tq* q, const steady_clock::time_point start )
4041{
4142 // pull elements off of the queue (earliest element first)
42- steady_clock::time_point start = steady_clock::now ();
43- for (int i = count - 1 ; i >= 0 ; --i)
43+ for (int i = cnt - 1 ; i >= 0 ; --i)
4444 {
4545 int j;
4646 q->pull (j);
4747 BOOST_TEST_EQ (i, j);
48- milliseconds elapsed = duration_cast<milliseconds>(steady_clock::now () - start);
49- milliseconds expected = milliseconds (i * 500 ) + seconds (count - i);
50- BOOST_TEST_GE (elapsed, expected - milliseconds (BOOST_THREAD_TEST_TIME_MS));
51- BOOST_TEST_LE (elapsed, expected + milliseconds (BOOST_THREAD_TEST_TIME_MS));
48+ const steady_clock::time_point expected = start + milliseconds (i * 500 ) + seconds (cnt - i);
49+ BOOST_TEST_GE (steady_clock::now (), expected - milliseconds (BOOST_THREAD_TEST_TIME_MS));
50+ BOOST_TEST_LE (steady_clock::now (), expected + milliseconds (BOOST_THREAD_TEST_TIME_MS));
5251 }
5352}
5453
55- void call_pull_until (sync_tq* q)
54+ void call_pull_until (sync_tq* q, const steady_clock::time_point start )
5655{
5756 // pull elements off of the queue (earliest element first)
58- steady_clock::time_point start = steady_clock::now ();
59- for (int i = count - 1 ; i >= 0 ; --i)
57+ for (int i = cnt - 1 ; i >= 0 ; --i)
6058 {
6159 int j;
6260 q->pull_until (steady_clock::now () + hours (1 ), j);
6361 BOOST_TEST_EQ (i, j);
64- milliseconds elapsed = duration_cast<milliseconds>(steady_clock::now () - start);
65- milliseconds expected = milliseconds (i * 500 ) + seconds (count - i);
66- BOOST_TEST_GE (elapsed, expected - milliseconds (BOOST_THREAD_TEST_TIME_MS));
67- BOOST_TEST_LE (elapsed, expected + milliseconds (BOOST_THREAD_TEST_TIME_MS));
62+ const steady_clock::time_point expected = start + milliseconds (i * 500 ) + seconds (cnt - i);
63+ BOOST_TEST_GE (steady_clock::now (), expected - milliseconds (BOOST_THREAD_TEST_TIME_MS));
64+ BOOST_TEST_LE (steady_clock::now (), expected + milliseconds (BOOST_THREAD_TEST_TIME_MS));
6865 }
6966}
7067
71- void call_pull_for (sync_tq* q)
68+ void call_pull_for (sync_tq* q, const steady_clock::time_point start )
7269{
7370 // pull elements off of the queue (earliest element first)
74- steady_clock::time_point start = steady_clock::now ();
75- for (int i = count - 1 ; i >= 0 ; --i)
71+ for (int i = cnt - 1 ; i >= 0 ; --i)
7672 {
7773 int j;
7874 q->pull_for (hours (1 ), j);
7975 BOOST_TEST_EQ (i, j);
80- milliseconds elapsed = duration_cast<milliseconds>(steady_clock::now () - start);
81- milliseconds expected = milliseconds (i * 500 ) + seconds (count - i);
82- BOOST_TEST_GE (elapsed, expected - milliseconds (BOOST_THREAD_TEST_TIME_MS));
83- BOOST_TEST_LE (elapsed, expected + milliseconds (BOOST_THREAD_TEST_TIME_MS));
76+ const steady_clock::time_point expected = start + milliseconds (i * 500 ) + seconds (cnt - i);
77+ BOOST_TEST_GE (steady_clock::now (), expected - milliseconds (BOOST_THREAD_TEST_TIME_MS));
78+ BOOST_TEST_LE (steady_clock::now (), expected + milliseconds (BOOST_THREAD_TEST_TIME_MS));
8479 }
8580}
8681
@@ -89,8 +84,9 @@ void test_push_while_pull()
8984 sync_tq tq;
9085 BOOST_TEST (tq.empty ());
9186 boost::thread_group tg;
92- tg.create_thread (boost::bind (call_push, &tq));
93- tg.create_thread (boost::bind (call_pull, &tq));
87+ const steady_clock::time_point start = steady_clock::now ();
88+ tg.create_thread (boost::bind (call_push, &tq, start));
89+ tg.create_thread (boost::bind (call_pull, &tq, start));
9490 tg.join_all ();
9591 BOOST_TEST (tq.empty ());
9692}
@@ -100,8 +96,9 @@ void test_push_while_pull_until()
10096 sync_tq tq;
10197 BOOST_TEST (tq.empty ());
10298 boost::thread_group tg;
103- tg.create_thread (boost::bind (call_push, &tq));
104- tg.create_thread (boost::bind (call_pull_until, &tq));
99+ const steady_clock::time_point start = steady_clock::now ();
100+ tg.create_thread (boost::bind (call_push, &tq, start));
101+ tg.create_thread (boost::bind (call_pull_until, &tq, start));
105102 tg.join_all ();
106103 BOOST_TEST (tq.empty ());
107104}
@@ -111,8 +108,9 @@ void test_push_while_pull_for()
111108 sync_tq tq;
112109 BOOST_TEST (tq.empty ());
113110 boost::thread_group tg;
114- tg.create_thread (boost::bind (call_push, &tq));
115- tg.create_thread (boost::bind (call_pull_for, &tq));
111+ const steady_clock::time_point start = steady_clock::now ();
112+ tg.create_thread (boost::bind (call_push, &tq, start));
113+ tg.create_thread (boost::bind (call_pull_for, &tq, start));
116114 tg.join_all ();
117115 BOOST_TEST (tq.empty ());
118116}
0 commit comments