@@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(manythreads)
65
65
size_t nTasks = microTasks.getQueueInfo (first, last);
66
66
BOOST_CHECK (nTasks == 0 );
67
67
68
- for (int i = 0 ; i < 100 ; i++ ) {
68
+ for (int i = 0 ; i < 100 ; ++i ) {
69
69
boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds (randomMsec (rng));
70
70
boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds (500 + randomMsec (rng));
71
71
int whichCounter = zeroToNine (rng);
@@ -112,4 +112,46 @@ BOOST_AUTO_TEST_CASE(manythreads)
112
112
BOOST_CHECK_EQUAL (counterSum, 200 );
113
113
}
114
114
115
+ BOOST_AUTO_TEST_CASE (singlethreadedscheduler_ordered)
116
+ {
117
+ CScheduler scheduler;
118
+
119
+ // each queue should be well ordered with respect to itself but not other queues
120
+ SingleThreadedSchedulerClient queue1 (&scheduler);
121
+ SingleThreadedSchedulerClient queue2 (&scheduler);
122
+
123
+ // create more threads than queues
124
+ // if the queues only permit execution of one task at once then
125
+ // the extra threads should effectively be doing nothing
126
+ // if they don't we'll get out of order behaviour
127
+ boost::thread_group threads;
128
+ for (int i = 0 ; i < 5 ; ++i) {
129
+ threads.create_thread (boost::bind (&CScheduler::serviceQueue, &scheduler));
130
+ }
131
+
132
+ // these are not atomic, if SinglethreadedSchedulerClient prevents
133
+ // parallel execution at the queue level no synchronization should be required here
134
+ int counter1 = 0 ;
135
+ int counter2 = 0 ;
136
+
137
+ // just simply count up on each queue - if execution is properly ordered then
138
+ // the callbacks should run in exactly the order in which they were enqueued
139
+ for (int i = 0 ; i < 100 ; ++i) {
140
+ queue1.AddToProcessQueue ([i, &counter1]() {
141
+ BOOST_CHECK_EQUAL (i, counter1++);
142
+ });
143
+
144
+ queue2.AddToProcessQueue ([i, &counter2]() {
145
+ BOOST_CHECK_EQUAL (i, counter2++);
146
+ });
147
+ }
148
+
149
+ // finish up
150
+ scheduler.stop (true );
151
+ threads.join_all ();
152
+
153
+ BOOST_CHECK_EQUAL (counter1, 100 );
154
+ BOOST_CHECK_EQUAL (counter2, 100 );
155
+ }
156
+
115
157
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments