mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
ConcurrentQueue::cancelPending: return the number of canceled jobs
The return value can be used to make ConcurrentQueueTest::cancelPending1UserThread() non-flaky. It may also be useful to non-testing code. Improve the performance of cancelPending() by locking the two mutexes separately and minimizing the locking time.
This commit is contained in:
parent
a72fdb9ca2
commit
b43e3383d5
@ -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<std::mutex> lockQueue(queueMutex);
|
||||
std::unique_lock<std::mutex> lockJobsLeft(jobsLeftMutex);
|
||||
jobsLeft -= _queue.size();
|
||||
_queue = {};
|
||||
decltype(_queue) oldQueue;
|
||||
{
|
||||
const std::lock_guard<std::mutex> 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<std::mutex> lock(jobsLeftMutex);
|
||||
jobsLeft -= size;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void waitAll()
|
||||
|
Loading…
Reference in New Issue
Block a user