Use "if constexpr" in ThreadPool

This commit is contained in:
Stephen Seo 2021-09-07 12:11:36 +09:00
parent eb638f325e
commit 20df53837b

View file

@ -33,7 +33,7 @@ public:
ThreadPool() : waitCount(0) { ThreadPool() : waitCount(0) {
isAlive.store(true); isAlive.store(true);
if(SIZE >= 2) { if constexpr(SIZE >= 2) {
for(unsigned int i = 0; i < SIZE; ++i) { for(unsigned int i = 0; i < SIZE; ++i) {
threads.emplace_back([] (std::atomic_bool *isAlive, threads.emplace_back([] (std::atomic_bool *isAlive,
std::condition_variable *cv, std::condition_variable *cv,
@ -79,7 +79,7 @@ public:
} }
~ThreadPool() { ~ThreadPool() {
if(SIZE >= 2) { if constexpr(SIZE >= 2) {
isAlive.store(false); isAlive.store(false);
std::this_thread::sleep_for(std::chrono::milliseconds(20)); std::this_thread::sleep_for(std::chrono::milliseconds(20));
cv.notify_all(); cv.notify_all();
@ -111,7 +111,7 @@ public:
waking one or all threads, depending on the given boolean parameter. waking one or all threads, depending on the given boolean parameter.
*/ */
void wakeThreads(bool wakeAll = true) { void wakeThreads(bool wakeAll = true) {
if(SIZE >= 2) { if constexpr(SIZE >= 2) {
// wake threads to pull functions from queue and run them // wake threads to pull functions from queue and run them
if(wakeAll) { if(wakeAll) {
cv.notify_all(); cv.notify_all();
@ -158,7 +158,7 @@ public:
If SIZE is less than 2, then this will always return true. If SIZE is less than 2, then this will always return true.
*/ */
bool isAllThreadsWaiting() { bool isAllThreadsWaiting() {
if(SIZE >= 2) { if constexpr(SIZE >= 2) {
std::lock_guard<std::mutex> lock(waitCountMutex); std::lock_guard<std::mutex> lock(waitCountMutex);
return waitCount == THREADCOUNT::value; return waitCount == THREADCOUNT::value;
} else { } else {