diff options
author | ReinUsesLisp <reinuseslisp@airmail.cc> | 2021-04-01 06:05:45 +0200 |
---|---|---|
committer | ReinUsesLisp <reinuseslisp@airmail.cc> | 2021-07-09 00:03:26 +0200 |
commit | bf5b5c1bf43946039d91f78253599c9996f86057 (patch) | |
tree | 2fcfd8f48297b03e3a9eb03cfe7594c7ead4edaa /src/common/thread_worker.cpp | |
parent | common: Add unique function (diff) | |
download | yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.gz yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.bz2 yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.lz yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.xz yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.tar.zst yuzu-bf5b5c1bf43946039d91f78253599c9996f86057.zip |
Diffstat (limited to 'src/common/thread_worker.cpp')
-rw-r--r-- | src/common/thread_worker.cpp | 46 |
1 files changed, 20 insertions, 26 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 @@ namespace Common { ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { - for (std::size_t i = 0; i < num_workers; ++i) - threads.emplace_back([this, thread_name{std::string{name}}] { - Common::SetCurrentThreadName(thread_name.c_str()); + const auto lambda = [this, thread_name{std::string{name}}] { + Common::SetCurrentThreadName(thread_name.c_str()); - // Wait for first request + while (!stop) { + UniqueFunction<void> task; { std::unique_lock lock{queue_mutex}; + if (requests.empty()) { + wait_condition.notify_all(); + } condition.wait(lock, [this] { return stop || !requests.empty(); }); - } - - while (true) { - std::function<void()> task; - - { - std::unique_lock lock{queue_mutex}; - condition.wait(lock, [this] { return stop || !requests.empty(); }); - if (stop || requests.empty()) { - return; - } - task = std::move(requests.front()); - requests.pop(); - - if (requests.empty()) { - wait_condition.notify_one(); - } + if (stop || requests.empty()) { + break; } - - task(); + task = std::move(requests.front()); + requests.pop(); } - }); + task(); + } + wait_condition.notify_all(); + }; + for (size_t i = 0; i < num_workers; ++i) { + threads.emplace_back(lambda); + } } ThreadWorker::~ThreadWorker() { @@ -51,10 +45,10 @@ ThreadWorker::~ThreadWorker() { } } -void ThreadWorker::QueueWork(std::function<void()>&& work) { +void ThreadWorker::QueueWork(UniqueFunction<void> work) { { std::unique_lock lock{queue_mutex}; - requests.emplace(work); + requests.emplace(std::move(work)); } condition.notify_one(); } |