common/thread_worker: Add wait for requests method
This commit is contained in:
parent
5edc96f4a4
commit
f28dd32275
|
@ -29,6 +29,10 @@ ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) {
|
||||||
}
|
}
|
||||||
task = std::move(requests.front());
|
task = std::move(requests.front());
|
||||||
requests.pop();
|
requests.pop();
|
||||||
|
|
||||||
|
if (requests.empty()) {
|
||||||
|
wait_condition.notify_one();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
task();
|
task();
|
||||||
|
@ -55,4 +59,9 @@ void ThreadWorker::QueueWork(std::function<void()>&& work) {
|
||||||
condition.notify_one();
|
condition.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThreadWorker::WaitForRequests() {
|
||||||
|
std::unique_lock lock{queue_mutex};
|
||||||
|
wait_condition.wait(lock, [this] { return stop || requests.empty(); });
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
|
@ -18,12 +18,14 @@ public:
|
||||||
explicit ThreadWorker(std::size_t num_workers, const std::string& name);
|
explicit ThreadWorker(std::size_t num_workers, const std::string& name);
|
||||||
~ThreadWorker();
|
~ThreadWorker();
|
||||||
void QueueWork(std::function<void()>&& work);
|
void QueueWork(std::function<void()>&& work);
|
||||||
|
void WaitForRequests();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
std::queue<std::function<void()>> requests;
|
std::queue<std::function<void()>> requests;
|
||||||
std::mutex queue_mutex;
|
std::mutex queue_mutex;
|
||||||
std::condition_variable condition;
|
std::condition_variable condition;
|
||||||
|
std::condition_variable wait_condition;
|
||||||
std::atomic_bool stop{};
|
std::atomic_bool stop{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue