mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-16 19:32:07 +00:00
f38a7aeabe
There are lots of thread pools and simple local-vs-global is not enough already, it is good to know which one in particular uses threads. Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
42 lines
1.0 KiB
C++
42 lines
1.0 KiB
C++
#include <Common/ThreadPool.h>
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
/** Reproduces bug in ThreadPool.
|
|
* It get stuck if we call 'wait' many times from many other threads simultaneously.
|
|
*/
|
|
|
|
|
|
namespace CurrentMetrics
|
|
{
|
|
extern const Metric LocalThread;
|
|
extern const Metric LocalThreadActive;
|
|
}
|
|
|
|
TEST(ThreadPool, ConcurrentWait)
|
|
{
|
|
auto worker = []
|
|
{
|
|
for (size_t i = 0; i < 100000000; ++i)
|
|
__asm__ volatile ("nop");
|
|
};
|
|
|
|
constexpr size_t num_threads = 4;
|
|
constexpr size_t num_jobs = 4;
|
|
|
|
ThreadPool pool(CurrentMetrics::LocalThread, CurrentMetrics::LocalThreadActive, num_threads);
|
|
|
|
for (size_t i = 0; i < num_jobs; ++i)
|
|
pool.scheduleOrThrowOnError(worker);
|
|
|
|
constexpr size_t num_waiting_threads = 4;
|
|
|
|
ThreadPool waiting_pool(CurrentMetrics::LocalThread, CurrentMetrics::LocalThreadActive, num_waiting_threads);
|
|
|
|
for (size_t i = 0; i < num_waiting_threads; ++i)
|
|
waiting_pool.scheduleOrThrowOnError([&pool] { pool.wait(); });
|
|
|
|
waiting_pool.wait();
|
|
}
|