add ThreadPool maxWorkers constructor argument
This commit is contained in:
parent
572ae9fa39
commit
a65cf6ed66
@ -103,14 +103,40 @@ namespace util {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
|
static constexpr int UNLIMITED = 0;
|
||||||
|
static constexpr int HALF = -2;
|
||||||
|
static constexpr int QUARTER = -4;
|
||||||
|
|
||||||
|
/// @brief Main thread pool constructor
|
||||||
|
/// @param name thread pool name (used in logger)
|
||||||
|
/// @param workersSupplier workers factory function
|
||||||
|
/// @param resultConsumer workers results consumer function
|
||||||
|
/// @param maxWorkers max number of workers. Special values: 0 is
|
||||||
|
/// unlimited, -2 is half of auto count, -4 is quarter.
|
||||||
ThreadPool(
|
ThreadPool(
|
||||||
std::string name,
|
std::string name,
|
||||||
supplier<std::shared_ptr<Worker<T, R>>> workersSupplier,
|
supplier<std::shared_ptr<Worker<T, R>>> workersSupplier,
|
||||||
consumer<R&> resultConsumer
|
consumer<R&> resultConsumer,
|
||||||
|
int maxWorkers=UNLIMITED
|
||||||
)
|
)
|
||||||
: logger(std::move(name)), resultConsumer(resultConsumer) {
|
: logger(std::move(name)), resultConsumer(resultConsumer) {
|
||||||
const uint num_threads = std::thread::hardware_concurrency();
|
uint numThreads = std::thread::hardware_concurrency();
|
||||||
for (uint i = 0; i < num_threads; i++) {
|
switch (maxWorkers) {
|
||||||
|
case UNLIMITED:
|
||||||
|
break;
|
||||||
|
case HALF:
|
||||||
|
numThreads = std::max(1U, numThreads);
|
||||||
|
break;
|
||||||
|
case QUARTER:
|
||||||
|
numThreads = std::max(1U, numThreads / 4);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
numThreads = std::max(
|
||||||
|
1U, std::min(numThreads, static_cast<uint>(maxWorkers))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (uint i = 0; i < numThreads; i++) {
|
||||||
threads.emplace_back(
|
threads.emplace_back(
|
||||||
&ThreadPool<T, R>::threadLoop, this, i, workersSupplier()
|
&ThreadPool<T, R>::threadLoop, this, i, workersSupplier()
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user