From 2655613543bb79e93b78f12881a3a4febf6b39f0 Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Thu, 18 Mar 2021 08:30:05 +0200 Subject: [PATCH] ConcurrentQueue: simplify the constructor implementation * threadCount argument: int => std::size_t to avoid implicit casting; * eliminate temporary empty std::thread objects; * replace a trivial lambda with a function pointer and its argument; * get rid of the unused dedicated loop counter. --- common/concurrent_queue.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/common/concurrent_queue.h b/common/concurrent_queue.h index 8cc8bf83..c434805e 100644 --- a/common/concurrent_queue.h +++ b/common/concurrent_queue.h @@ -14,16 +14,13 @@ namespace YACReader { class ConcurrentQueue { public: - explicit ConcurrentQueue(int threadCount) + explicit ConcurrentQueue(std::size_t threadCount) : jobsLeft(0), bailout(false) { - threads = std::vector(threadCount); - for (int index = 0; index < threadCount; ++index) { - threads[index] = std::thread([this] { - this->nextJob(); - }); - } + threads.reserve(threadCount); + for (; threadCount != 0; --threadCount) + threads.emplace_back(&ConcurrentQueue::nextJob, this); } ~ConcurrentQueue()