From 05b384ed6dbfeeb9662c04a00c4fc6f3af06955e Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Thu, 18 Mar 2021 11:30:55 +0200 Subject: [PATCH] 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. --- common/concurrent_queue.cpp | 2 +- common/concurrent_queue.h | 6 +++--- tests/concurrent_queue_test/concurrent_queue_test.cpp | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common/concurrent_queue.cpp b/common/concurrent_queue.cpp index fa8eba24..a762f4db 100644 --- a/common/concurrent_queue.cpp +++ b/common/concurrent_queue.cpp @@ -59,7 +59,7 @@ std::size_t ConcurrentQueue::cancelPending() return size; } -void ConcurrentQueue::waitAll() +void ConcurrentQueue::waitAll() const { std::unique_lock lock(jobsLeftMutex); _waitVar.wait(lock, [this] { return jobsLeft == 0; }); diff --git a/common/concurrent_queue.h b/common/concurrent_queue.h index 4d3c4f23..380f6d5d 100644 --- a/common/concurrent_queue.h +++ b/common/concurrent_queue.h @@ -33,7 +33,7 @@ public: std::size_t cancelPending(); //! @brief Blocks the current thread until all enqueued jobs are completed. - void waitAll(); + void waitAll() const; private: //! @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() bool bailout = false; //!< @invariant is false until the destructor is called. std::condition_variable jobAvailableVar; - std::condition_variable _waitVar; - std::mutex jobsLeftMutex; + mutable std::condition_variable _waitVar; + mutable std::mutex jobsLeftMutex; std::mutex queueMutex; void nextJob(); diff --git a/tests/concurrent_queue_test/concurrent_queue_test.cpp b/tests/concurrent_queue_test/concurrent_queue_test.cpp index 11ce55b6..b09d05f6 100644 --- a/tests/concurrent_queue_test/concurrent_queue_test.cpp +++ b/tests/concurrent_queue_test/concurrent_queue_test.cpp @@ -202,7 +202,7 @@ std::size_t cancelAndPrint(ConcurrentQueue &queue, const QueueControlMessagePrin return canceledCount; } -void waitAndPrint(ConcurrentQueue &queue, const QueueControlMessagePrinter &printer) +void waitAndPrint(const ConcurrentQueue &queue, const QueueControlMessagePrinter &printer) { printer.printBeginWaitingMessage(); queue.waitAll();