Make ConcurrentQueue::waitAll() const

This member function does not affect the logical state of the class.
Making std::condition_variable and std::mutex data members mutable is
idiomatic as these classes are thread-safe synchronization primitives.
This commit is contained in:
Igor Kushnir 2021-03-18 11:30:55 +02:00 committed by Luis Ángel San Martín
parent 72c78d0164
commit 05b384ed6d
3 changed files with 5 additions and 5 deletions

View File

@ -59,7 +59,7 @@ std::size_t ConcurrentQueue::cancelPending()
return size; return size;
} }
void ConcurrentQueue::waitAll() void ConcurrentQueue::waitAll() const
{ {
std::unique_lock<std::mutex> lock(jobsLeftMutex); std::unique_lock<std::mutex> lock(jobsLeftMutex);
_waitVar.wait(lock, [this] { return jobsLeft == 0; }); _waitVar.wait(lock, [this] { return jobsLeft == 0; });

View File

@ -33,7 +33,7 @@ public:
std::size_t cancelPending(); std::size_t cancelPending();
//! @brief Blocks the current thread until all enqueued jobs are completed. //! @brief Blocks the current thread until all enqueued jobs are completed.
void waitAll(); void waitAll() const;
private: private:
//! @invariant all worker threads are joinable until the destructor is called. //! @invariant all worker threads are joinable until the destructor is called.
@ -42,8 +42,8 @@ private:
std::size_t jobsLeft = 0; //!< @invariant jobsLeft >= _queue.size() std::size_t jobsLeft = 0; //!< @invariant jobsLeft >= _queue.size()
bool bailout = false; //!< @invariant is false until the destructor is called. bool bailout = false; //!< @invariant is false until the destructor is called.
std::condition_variable jobAvailableVar; std::condition_variable jobAvailableVar;
std::condition_variable _waitVar; mutable std::condition_variable _waitVar;
std::mutex jobsLeftMutex; mutable std::mutex jobsLeftMutex;
std::mutex queueMutex; std::mutex queueMutex;
void nextJob(); void nextJob();

View File

@ -202,7 +202,7 @@ std::size_t cancelAndPrint(ConcurrentQueue &queue, const QueueControlMessagePrin
return canceledCount; return canceledCount;
} }
void waitAndPrint(ConcurrentQueue &queue, const QueueControlMessagePrinter &printer) void waitAndPrint(const ConcurrentQueue &queue, const QueueControlMessagePrinter &printer)
{ {
printer.printBeginWaitingMessage(); printer.printBeginWaitingMessage();
queue.waitAll(); queue.waitAll();