File tree Expand file tree Collapse file tree 1 file changed +7
-9
lines changed Expand file tree Collapse file tree 1 file changed +7
-9
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ void stress() {
26
26
constexpr int PRODUCERS = 3 ;
27
27
constexpr int CONSUMERS = 3 ;
28
28
constexpr unsigned N = 1000000 ;
29
+ using T = typename Queue::value_type;
30
+ constexpr T STOP = -1 ;
29
31
30
32
Queue q;
31
33
Barrier barrier;
@@ -34,7 +36,7 @@ void stress() {
34
36
for (unsigned i = 0 ; i < PRODUCERS; ++i)
35
37
producers[i] = std::thread ([&q, &barrier, N=N]() {
36
38
barrier.wait ();
37
- for (unsigned n = N; n; --n)
39
+ for (T n = N; n; --n)
38
40
q.push (n);
39
41
});
40
42
@@ -44,31 +46,27 @@ void stress() {
44
46
consumers[i] = std::thread ([&q, &barrier, &r = results[i]]() {
45
47
barrier.wait ();
46
48
uint64_t result = 0 ;
47
- for (;;) {
48
- unsigned n = q.pop ();
49
+ for (T n; (n = q.pop ()) != STOP;)
49
50
result += n;
50
- if (n == 1 )
51
- break ;
52
- }
53
51
r = result;
54
52
});
55
53
56
54
barrier.release (PRODUCERS + CONSUMERS);
57
-
58
55
for (auto & t : producers)
59
56
t.join ();
57
+ for (int i = CONSUMERS; i--;)
58
+ q.push (STOP);
60
59
for (auto & t : consumers)
61
60
t.join ();
62
61
63
62
constexpr uint64_t expected_result = (N + 1 ) / 2 . * N;
64
-
65
63
uint64_t result = 0 ;
66
64
for (auto & r : results) {
67
65
BOOST_WARN_GT (r, (expected_result / CONSUMERS) / 10 ); // Make sure a consumer didn't starve. False positives are possible here.
68
66
result += r;
69
67
}
70
68
71
- int64_t result_diff = result / CONSUMERS - expected_result;
69
+ int64_t result_diff = result / PRODUCERS - expected_result;
72
70
BOOST_CHECK_EQUAL (result_diff, 0 );
73
71
}
74
72
You can’t perform that action at this time.
0 commit comments