Test where each thread calls Wait() in a tight loop, to exercise cases where calls to Wait() from different cycles can overlap. E.g. if there are two threads and a single processor, the following timing is possible: 1. Thread 1 runs, calls Wait() and blocks. 2. Thread 2 runs, calls Wait(), and notifies thread 1. 3. Thread 2 continues running, returns from Wait(), then calls Wait() again and blocks
| 79 | // At step 3, thread 1 and 2 are both in Wait() but are in different cycles, hence the |
| 80 | // overlapping of the cycles. |
| 81 | void OverlapTest(int num_threads, int num_iters) { |
| 82 | CyclicBarrier barrier(num_threads); |
| 83 | int counter = 0; |
| 84 | thread_group threads; |
| 85 | for (int i = 0; i < num_threads; ++i) { |
| 86 | threads.add_thread(new thread([&]() { |
| 87 | for (int j = 0; j < num_iters; ++j) { |
| 88 | EXPECT_OK(barrier.Wait([&counter]() { |
| 89 | ++counter; |
| 90 | return Status::OK(); |
| 91 | })); |
| 92 | } |
| 93 | })); |
| 94 | } |
| 95 | threads.join_all(); |
| 96 | // Counter should have been incremented by last arriving threads. |
| 97 | EXPECT_EQ(num_iters, counter); |
| 98 | } |
| 99 | |
| 100 | // Test many iterations of the barrier. |
| 101 | TEST(CyclicBarrierTest, Overlap) { |