diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-09-20 07:48:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-20 07:48:49 -0700 |
commit | cfd070c4e0eed92737794dfcf7620393fcf25537 (patch) | |
tree | b1a13a692f4b0e6bea11aa973c36b0f81641c6da /src | |
parent | c3a583cdd59c4ed8e67468caa7d2af74791c01b7 (diff) | |
download | qtlocation-mapboxgl-cfd070c4e0eed92737794dfcf7620393fcf25537.tar.gz |
[core] Fix ThreadPool race condition (#6388)
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/actor/thread_pool.cpp | 17 | ||||
-rw-r--r-- | src/mbgl/actor/thread_pool.hpp | 3 |
2 files changed, 13 insertions, 7 deletions
diff --git a/src/mbgl/actor/thread_pool.cpp b/src/mbgl/actor/thread_pool.cpp index 89ca1b72f0..2995ed26ad 100644 --- a/src/mbgl/actor/thread_pool.cpp +++ b/src/mbgl/actor/thread_pool.cpp @@ -11,10 +11,10 @@ ThreadPool::ThreadPool(std::size_t count) { std::unique_lock<std::mutex> lock(mutex); cv.wait(lock, [this] { - return !queue.empty() || terminate.load(); + return !queue.empty() || terminate; }); - if (terminate.load()) { + if (terminate) { return; } @@ -31,7 +31,11 @@ ThreadPool::ThreadPool(std::size_t count) { } ThreadPool::~ThreadPool() { - terminate.store(true); + { + std::lock_guard<std::mutex> lock(mutex); + terminate = true; + } + cv.notify_all(); for (auto& thread : threads) { @@ -40,8 +44,11 @@ ThreadPool::~ThreadPool() { } void ThreadPool::schedule(std::weak_ptr<Mailbox> mailbox) { - std::lock_guard<std::mutex> lock(mutex); - queue.push(mailbox); + { + std::lock_guard<std::mutex> lock(mutex); + queue.push(mailbox); + } + cv.notify_one(); } diff --git a/src/mbgl/actor/thread_pool.hpp b/src/mbgl/actor/thread_pool.hpp index 7e0b40f4e3..a14d16d771 100644 --- a/src/mbgl/actor/thread_pool.hpp +++ b/src/mbgl/actor/thread_pool.hpp @@ -2,7 +2,6 @@ #include <mbgl/actor/scheduler.hpp> -#include <atomic> #include <condition_variable> #include <mutex> #include <queue> @@ -22,7 +21,7 @@ private: std::queue<std::weak_ptr<Mailbox>> queue; std::mutex mutex; std::condition_variable cv; - std::atomic_bool terminate { false }; + bool terminate { false }; }; } // namespace mbgl |