Commit 15e1b703 authored by a-chmil's avatar a-chmil
Browse files

Added callback on_empty in thread_pool

parent 14416a11
......@@ -22,6 +22,8 @@ public:
// add job
void submit(std::function<void()>);
void on_empty(std::function<void()>);
virtual std::string to_string() const;
private:
......@@ -29,6 +31,7 @@ private:
std::condition_variable cv_;
std::vector<std::thread*> threads_;
std::queue<std::function<void()> > jobs_;
std::function<void()> on_empty_handler_;
size_t running_jobs_;
bool stop_flag_;
......
......@@ -47,6 +47,9 @@ int RTS::run()
comm_->barrier();
std::unique_lock<std::mutex> lk(m_);
pool_.on_empty([this](){
steal_req();
});
pool_.start(conf_->get_worker_threads_count());
finished_flag_=false;
......
......@@ -5,7 +5,7 @@
#include "common.h"
ThreadPool::ThreadPool()
: running_jobs_(0), stop_flag_(false)
: running_jobs_(0), stop_flag_(false), on_empty_handler_([](){})
{
stop();
}
......@@ -61,6 +61,13 @@ void ThreadPool::submit(std::function<void()> job)
cv_.notify_one();
}
void ThreadPool::on_empty(std::function<void()> on_empty_handler)
{
std::lock_guard<std::mutex> lk(m_);
on_empty_handler_ = on_empty_handler;
}
std::string ThreadPool::to_string() const
{
std::lock_guard<std::mutex> lk(m_);
......@@ -77,6 +84,7 @@ void ThreadPool::routine()
while (!stop_flag_ || !jobs_.empty() || running_jobs_>0) {
if (jobs_.empty()) {
on_empty_handler_();
cv_.wait(lk);
continue;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment