diff --git a/common/concurrent_queue.h b/common/concurrent_queue.h index 300f783d..d7031dbf 100644 --- a/common/concurrent_queue.h +++ b/common/concurrent_queue.h @@ -43,12 +43,23 @@ public: jobAvailableVar.notify_one(); } - void cancelPending() + //! @brief Cancels all jobs that have not been picked up by worker threads yet. + //! @return The number of jobs that were canceled. + std::size_t cancelPending() { - std::unique_lock lockQueue(queueMutex); - std::unique_lock lockJobsLeft(jobsLeftMutex); - jobsLeft -= _queue.size(); - _queue = {}; + decltype(_queue) oldQueue; + { + const std::lock_guard lock(queueMutex); + // The mutex locking time is lower with swap() compared to assigning a + // temporary (which destroys _queue's elements and deallocates memory). + _queue.swap(oldQueue); + } + const auto size = oldQueue.size(); + { + const std::lock_guard lock(jobsLeftMutex); + jobsLeft -= size; + } + return size; } void waitAll()