diff options
Diffstat (limited to '')
| -rw-r--r-- | src/common/thread_worker.cpp | 46 | ||||
| -rw-r--r-- | src/common/thread_worker.h | 6 |
2 files changed, 24 insertions, 28 deletions
diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp index 745918c7e..f4d8bb0f0 100644 --- a/src/common/thread_worker.cpp +++ b/src/common/thread_worker.cpp | |||
| @@ -8,36 +8,30 @@ | |||
| 8 | namespace Common { | 8 | namespace Common { |
| 9 | 9 | ||
| 10 | ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { | 10 | ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { |
| 11 | for (std::size_t i = 0; i < num_workers; ++i) | 11 | const auto lambda = [this, thread_name{std::string{name}}] { |
| 12 | threads.emplace_back([this, thread_name{std::string{name}}] { | 12 | Common::SetCurrentThreadName(thread_name.c_str()); |
| 13 | Common::SetCurrentThreadName(thread_name.c_str()); | ||
| 14 | 13 | ||
| 15 | // Wait for first request | 14 | while (!stop) { |
| 15 | UniqueFunction<void> task; | ||
| 16 | { | 16 | { |
| 17 | std::unique_lock lock{queue_mutex}; | 17 | std::unique_lock lock{queue_mutex}; |
| 18 | if (requests.empty()) { | ||
| 19 | wait_condition.notify_all(); | ||
| 20 | } | ||
| 18 | condition.wait(lock, [this] { return stop || !requests.empty(); }); | 21 | condition.wait(lock, [this] { return stop || !requests.empty(); }); |
| 19 | } | 22 | if (stop || requests.empty()) { |
| 20 | 23 | break; | |
| 21 | while (true) { | ||
| 22 | std::function<void()> task; | ||
| 23 | |||
| 24 | { | ||
| 25 | std::unique_lock lock{queue_mutex}; | ||
| 26 | condition.wait(lock, [this] { return stop || !requests.empty(); }); | ||
| 27 | if (stop || requests.empty()) { | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | task = std::move(requests.front()); | ||
| 31 | requests.pop(); | ||
| 32 | |||
| 33 | if (requests.empty()) { | ||
| 34 | wait_condition.notify_one(); | ||
| 35 | } | ||
| 36 | } | 24 | } |
| 37 | 25 | task = std::move(requests.front()); | |
| 38 | task(); | 26 | requests.pop(); |
| 39 | } | 27 | } |
| 40 | }); | 28 | task(); |
| 29 | } | ||
| 30 | wait_condition.notify_all(); | ||
| 31 | }; | ||
| 32 | for (size_t i = 0; i < num_workers; ++i) { | ||
| 33 | threads.emplace_back(lambda); | ||
| 34 | } | ||
| 41 | } | 35 | } |
| 42 | 36 | ||
| 43 | ThreadWorker::~ThreadWorker() { | 37 | ThreadWorker::~ThreadWorker() { |
| @@ -51,10 +45,10 @@ ThreadWorker::~ThreadWorker() { | |||
| 51 | } | 45 | } |
| 52 | } | 46 | } |
| 53 | 47 | ||
| 54 | void ThreadWorker::QueueWork(std::function<void()>&& work) { | 48 | void ThreadWorker::QueueWork(UniqueFunction<void> work) { |
| 55 | { | 49 | { |
| 56 | std::unique_lock lock{queue_mutex}; | 50 | std::unique_lock lock{queue_mutex}; |
| 57 | requests.emplace(work); | 51 | requests.emplace(std::move(work)); |
| 58 | } | 52 | } |
| 59 | condition.notify_one(); | 53 | condition.notify_one(); |
| 60 | } | 54 | } |
diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h index 7a6756eb5..7e2b04a07 100644 --- a/src/common/thread_worker.h +++ b/src/common/thread_worker.h | |||
| @@ -11,18 +11,20 @@ | |||
| 11 | #include <vector> | 11 | #include <vector> |
| 12 | #include <queue> | 12 | #include <queue> |
| 13 | 13 | ||
| 14 | #include "common/unique_function.h" | ||
| 15 | |||
| 14 | namespace Common { | 16 | namespace Common { |
| 15 | 17 | ||
| 16 | class ThreadWorker final { | 18 | class ThreadWorker final { |
| 17 | public: | 19 | public: |
| 18 | explicit ThreadWorker(std::size_t num_workers, const std::string& name); | 20 | explicit ThreadWorker(std::size_t num_workers, const std::string& name); |
| 19 | ~ThreadWorker(); | 21 | ~ThreadWorker(); |
| 20 | void QueueWork(std::function<void()>&& work); | 22 | void QueueWork(UniqueFunction<void> work); |
| 21 | void WaitForRequests(); | 23 | void WaitForRequests(); |
| 22 | 24 | ||
| 23 | private: | 25 | private: |
| 24 | std::vector<std::thread> threads; | 26 | std::vector<std::thread> threads; |
| 25 | std::queue<std::function<void()>> requests; | 27 | std::queue<UniqueFunction<void>> requests; |
| 26 | std::mutex queue_mutex; | 28 | std::mutex queue_mutex; |
| 27 | std::condition_variable condition; | 29 | std::condition_variable condition; |
| 28 | std::condition_variable wait_condition; | 30 | std::condition_variable wait_condition; |