mirror of
https://github.com/YACReader/yacreader
synced 2025-06-04 01:28:55 -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();
|
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);
|
decltype(_queue) oldQueue;
|
||||||
std::unique_lock<std::mutex> lockJobsLeft(jobsLeftMutex);
|
{
|
||||||
jobsLeft -= _queue.size();
|
const std::lock_guard<std::mutex> lock(queueMutex);
|
||||||
_queue = {};
|
// 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()
|
void waitAll()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user