ConcurrentQueue::cancelPending: don't reset jobsLeft to 0

Worker threads may well be executing jobs while this function is being
called. If ConcurrentQueue::waitAll() is called soon enough after
cancelPending(), the worker threads may still be running, but waitAll()
would return immediately as jobsLeft would be nonpositive.

Subtracting _queue.size() from jobsLeft sets this variable to the number
of worker threads that are executing jobs at the moment.

ConcurrentQueueTest::cancelPending1UserThread() passes most of the time
now. But it still fails occasionally because it depends on the timing of
thread scheduling, which is unreliable.
This commit is contained in:
Igor Kushnir 2021-03-12 12:57:25 +02:00 committed by Luis Ángel San Martín
parent 4bbd16c3b3
commit a72fdb9ca2

View File

@ -47,8 +47,8 @@ public:
{
std::unique_lock<std::mutex> lockQueue(queueMutex);
std::unique_lock<std::mutex> lockJobsLeft(jobsLeftMutex);
_queue = std::queue<std::function<void(void)>>();
jobsLeft = 0;
jobsLeft -= _queue.size();
_queue = {};
}
void waitAll()