std::condition_variable::notify_one/all() should be called after unlocking mutex (#1448)

* Move next job in task queue rather than copy

* Notify waiting thread after unlocking mutex

* Add unit test for TaskQueue

* Don't use C++14 feature in test code
This commit is contained in:
Jiwoo Park 2022-12-10 07:37:48 +09:00 committed by GitHub
parent 8f32271e8c
commit 58cffd3223
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -549,8 +549,11 @@ public:
~ThreadPool() override = default;
void enqueue(std::function<void()> fn) override {
std::unique_lock<std::mutex> lock(mutex_);
jobs_.push_back(std::move(fn));
{
std::unique_lock<std::mutex> lock(mutex_);
jobs_.push_back(std::move(fn));
}
cond_.notify_one();
}
@ -559,9 +562,10 @@ public:
{
std::unique_lock<std::mutex> lock(mutex_);
shutdown_ = true;
cond_.notify_all();
}
cond_.notify_all();
// Join...
for (auto &t : threads_) {
t.join();
@ -583,7 +587,7 @@ private:
if (pool_.shutdown_ && pool_.jobs_.empty()) { break; }
fn = pool_.jobs_.front();
fn = std::move(pool_.jobs_.front());
pool_.jobs_.pop_front();
}