util-threadpool: Fix rare double-execute bug

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-04-11 02:34:34 +02:00 committed by Michael Fabian Dirks
parent 7fbbd39bf5
commit 05d69e3c59

View file

@ -51,15 +51,15 @@ void util::threadpool::push(threadpool_function_t fn, std::shared_ptr<void> data
void util::threadpool::work() void util::threadpool::work()
{ {
while (!_worker_stop) {
std::pair<threadpool_function_t, std::shared_ptr<void>> work; std::pair<threadpool_function_t, std::shared_ptr<void>> work;
while (!_worker_stop) {
// Wait for more work, or immediately continue if there is still work to do. // Wait for more work, or immediately continue if there is still work to do.
{ {
std::unique_lock<std::mutex> lock(_tasks_lock); std::unique_lock<std::mutex> lock(_tasks_lock);
if (_tasks.size() == 0) if (_tasks.size() == 0)
_tasks_cv.wait(lock, [this]() { return _worker_stop || _tasks.size() > 0; }); _tasks_cv.wait(lock, [this]() { return _worker_stop || _tasks.size() > 0; });
if (_tasks.size() == 0) if (_worker_stop || (_tasks.size() == 0))
continue; continue;
work = _tasks.front(); work = _tasks.front();
_tasks.pop_front(); _tasks.pop_front();