ClickHouse/src/Common/tests/gtest_thread_pool_concurrent_wait.cpp

35 lines
787 B
C++
Raw Normal View History

#include <Common/ThreadPool.h>
2016-09-26 10:15:33 +00:00
2019-06-29 23:23:53 +00:00
#include <gtest/gtest.h>
2016-09-26 10:15:33 +00:00
/** Reproduces bug in ThreadPool.
* It get stuck if we call 'wait' many times from many other threads simultaneously.
*/
2019-06-29 23:23:53 +00:00
TEST(ThreadPool, ConcurrentWait)
2016-09-26 10:15:33 +00:00
{
auto worker = []
{
for (size_t i = 0; i < 100000000; ++i)
__asm__ volatile ("nop");
};
2016-09-26 10:15:33 +00:00
constexpr size_t num_threads = 4;
constexpr size_t num_jobs = 4;
2016-09-26 10:15:33 +00:00
ThreadPool pool(num_threads);
2016-09-26 10:15:33 +00:00
for (size_t i = 0; i < num_jobs; ++i)
pool.scheduleOrThrowOnError(worker);
2016-09-26 10:15:33 +00:00
constexpr size_t num_waiting_threads = 4;
2016-09-26 10:15:33 +00:00
ThreadPool waiting_pool(num_waiting_threads);
2016-09-26 10:15:33 +00:00
for (size_t i = 0; i < num_waiting_threads; ++i)
waiting_pool.scheduleOrThrowOnError([&pool] { pool.wait(); });
2016-09-26 10:15:33 +00:00
waiting_pool.wait();
2016-09-26 10:15:33 +00:00
}