|
@@ -4,7 +4,7 @@
|
|
|
namespace hainan {
|
|
namespace hainan {
|
|
|
|
|
|
|
|
thread_pool::thread_pool() :
|
|
thread_pool::thread_pool() :
|
|
|
- num_(0), running_(false), work_num_(0)
|
|
|
|
|
|
|
+ num(0), running(false), work_num(0)
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
thread_pool::~thread_pool()
|
|
thread_pool::~thread_pool()
|
|
@@ -13,27 +13,27 @@ thread_pool::~thread_pool()
|
|
|
|
|
|
|
|
void thread_pool::start()
|
|
void thread_pool::start()
|
|
|
{
|
|
{
|
|
|
- running_ = true;
|
|
|
|
|
- for (int i = 0; i < num_; ++i)
|
|
|
|
|
|
|
+ running = true;
|
|
|
|
|
+ for (int i = 0; i < num; ++i)
|
|
|
{
|
|
{
|
|
|
thread_ptr t(new boost::thread(
|
|
thread_ptr t(new boost::thread(
|
|
|
boost::bind(&thread_pool::runner, this)));
|
|
boost::bind(&thread_pool::runner, this)));
|
|
|
- threads_.push_back(t);
|
|
|
|
|
|
|
+ threads.push_back(t);
|
|
|
t->detach();
|
|
t->detach();
|
|
|
}
|
|
}
|
|
|
- work_num_ = num_;
|
|
|
|
|
|
|
+ work_num = num;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void thread_pool::stop()
|
|
void thread_pool::stop()
|
|
|
{
|
|
{
|
|
|
VLOG(3)<< "Stop";
|
|
VLOG(3)<< "Stop";
|
|
|
- boost::mutex::scoped_lock lock(mutex_);
|
|
|
|
|
- running_ = false;
|
|
|
|
|
- cond_.notify_all();
|
|
|
|
|
- while (work_num_ > 0)
|
|
|
|
|
|
|
+ boost::mutex::scoped_lock lock(mutex);
|
|
|
|
|
+ running = false;
|
|
|
|
|
+ cond.notify_all();
|
|
|
|
|
+ while (work_num > 0)
|
|
|
{
|
|
{
|
|
|
- VLOG(3) << "done tasks size = " << tasks_.size();
|
|
|
|
|
- done_.wait(lock);
|
|
|
|
|
|
|
+ VLOG(3) << "done tasks size = " << tasks.size();
|
|
|
|
|
+ done.wait(lock);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -46,23 +46,23 @@ void thread_pool::runner()
|
|
|
boost::function<void (void)> task;
|
|
boost::function<void (void)> task;
|
|
|
{
|
|
{
|
|
|
VLOG(3) << "loop lock";
|
|
VLOG(3) << "loop lock";
|
|
|
- boost::mutex::scoped_lock lock(mutex_);
|
|
|
|
|
|
|
+ boost::mutex::scoped_lock lock(mutex);
|
|
|
VLOG(3) << "loop lock ok";
|
|
VLOG(3) << "loop lock ok";
|
|
|
- while (running_ && tasks_.empty())
|
|
|
|
|
|
|
+ while (running && tasks.empty())
|
|
|
{
|
|
{
|
|
|
- cond_.wait(lock);
|
|
|
|
|
|
|
+ cond.wait(lock);
|
|
|
VLOG(3) << "cond";
|
|
VLOG(3) << "cond";
|
|
|
}
|
|
}
|
|
|
- if (!tasks_.empty())
|
|
|
|
|
|
|
+ if (!tasks.empty())
|
|
|
{
|
|
{
|
|
|
VLOG(3) << "fetch task";
|
|
VLOG(3) << "fetch task";
|
|
|
- task = tasks_.front();
|
|
|
|
|
- tasks_.pop_front();
|
|
|
|
|
|
|
+ task = tasks.front();
|
|
|
|
|
+ tasks.pop_front();
|
|
|
}
|
|
}
|
|
|
- continued = running_ || !tasks_.empty();
|
|
|
|
|
|
|
+ continued = running || !tasks.empty();
|
|
|
VLOG(3) << "continued = " << continued
|
|
VLOG(3) << "continued = " << continued
|
|
|
- << "running = " << running_
|
|
|
|
|
- << " tasks size = " << tasks_.size();
|
|
|
|
|
|
|
+ << "running = " << running
|
|
|
|
|
+ << " tasks size = " << tasks.size();
|
|
|
VLOG(3) << "loop unlock";
|
|
VLOG(3) << "loop unlock";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -71,10 +71,10 @@ void thread_pool::runner()
|
|
|
task();
|
|
task();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if (__sync_sub_and_fetch(&work_num_, 1) == 0)
|
|
|
|
|
|
|
+ if (__sync_sub_and_fetch(&work_num, 1) == 0)
|
|
|
{
|
|
{
|
|
|
- VLOG(3) << "work_num = " << work_num_;
|
|
|
|
|
- done_.notify_one();
|
|
|
|
|
|
|
+ VLOG(3) << "work_num = " << work_num;
|
|
|
|
|
+ done.notify_one();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -82,21 +82,21 @@ bool thread_pool::push_task(boost::function<void (void)> task)
|
|
|
{
|
|
{
|
|
|
VLOG(3) << "push task";
|
|
VLOG(3) << "push task";
|
|
|
{
|
|
{
|
|
|
- boost::mutex::scoped_lock lock(mutex_);
|
|
|
|
|
- if (!running_)
|
|
|
|
|
|
|
+ boost::mutex::scoped_lock lock(mutex);
|
|
|
|
|
+ if (!running)
|
|
|
{
|
|
{
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- tasks_.push_back(task);
|
|
|
|
|
|
|
+ tasks.push_back(task);
|
|
|
}
|
|
}
|
|
|
VLOG(3) << "push task unlock";
|
|
VLOG(3) << "push task unlock";
|
|
|
- cond_.notify_one();
|
|
|
|
|
|
|
+ cond.notify_one();
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void thread_pool::set_num(int n)
|
|
|
|
|
|
|
+void thread_pool::set_num(int num)
|
|
|
{
|
|
{
|
|
|
- num_ = n;
|
|
|
|
|
|
|
+ this->num = num;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} // namespace hainan
|
|
} // namespace hainan
|