1#include <Common/ThreadPool.h>
2
3#include <gtest/gtest.h>
4
5/** Reproduces bug in ThreadPool.
6 * It get stuck if we call 'wait' many times from many other threads simultaneously.
7 */
8
9
10TEST(ThreadPool, ConcurrentWait)
11{
12 auto worker = []
13 {
14 for (size_t i = 0; i < 100000000; ++i)
15 __asm__ volatile ("nop");
16 };
17
18 constexpr size_t num_threads = 4;
19 constexpr size_t num_jobs = 4;
20
21 ThreadPool pool(num_threads);
22
23 for (size_t i = 0; i < num_jobs; ++i)
24 pool.scheduleOrThrowOnError(worker);
25
26 constexpr size_t num_waiting_threads = 4;
27
28 ThreadPool waiting_pool(num_waiting_threads);
29
30 for (size_t i = 0; i < num_waiting_threads; ++i)
31 waiting_pool.scheduleOrThrowOnError([&pool] { pool.wait(); });
32
33 waiting_pool.wait();
34}
35