|
|
@@ -4,7 +4,7 @@
|
|
|
namespace hainan {
|
|
|
|
|
|
thread_pool::thread_pool() :
|
|
|
- num(0), running(false), work_num(0)
|
|
|
+ num_(0), running_(false), work_num_(0)
|
|
|
{
|
|
|
}
|
|
|
thread_pool::~thread_pool()
|
|
|
@@ -13,26 +13,27 @@ thread_pool::~thread_pool()
|
|
|
|
|
|
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 thread(bind(&thread_pool::runner, this)));
|
|
|
- threads.push_back(t);
|
|
|
+ thread_ptr t(new boost::thread(
|
|
|
+ boost::bind(&thread_pool::runner, this)));
|
|
|
+ threads_.push_back(t);
|
|
|
t->detach();
|
|
|
}
|
|
|
- work_num = num;
|
|
|
+ work_num_ = num_;
|
|
|
}
|
|
|
|
|
|
void thread_pool::stop()
|
|
|
{
|
|
|
VLOG(3)<< "Stop";
|
|
|
- mutex::scoped_lock lock(mtx);
|
|
|
- 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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -42,26 +43,26 @@ void thread_pool::runner()
|
|
|
bool continued = true;
|
|
|
while(continued)
|
|
|
{
|
|
|
- function<void (void)> task;
|
|
|
+ boost::function<void (void)> task;
|
|
|
{
|
|
|
VLOG(3) << "loop lock";
|
|
|
- mutex::scoped_lock lock(mtx);
|
|
|
+ boost::mutex::scoped_lock lock(mutex_);
|
|
|
VLOG(3) << "loop lock ok";
|
|
|
- while(running && tasks.empty())
|
|
|
+ while(running_ && tasks_.empty())
|
|
|
{
|
|
|
- cond.wait(lock);
|
|
|
+ cond_.wait(lock);
|
|
|
VLOG(3) << "cond";
|
|
|
}
|
|
|
- if(!tasks.empty())
|
|
|
+ if(!tasks_.empty())
|
|
|
{
|
|
|
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
|
|
|
- << "running = " << running
|
|
|
- << " tasks size = " << tasks.size();
|
|
|
+ << "running = " << running_
|
|
|
+ << " tasks size = " << tasks_.size();
|
|
|
VLOG(3) << "loop unlock";
|
|
|
}
|
|
|
|
|
|
@@ -70,32 +71,32 @@ void thread_pool::runner()
|
|
|
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();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-bool thread_pool::push_task(function<void (void)> task)
|
|
|
+bool thread_pool::push_task(boost::function<void (void)> task)
|
|
|
{
|
|
|
VLOG(3) << "push task";
|
|
|
{
|
|
|
- mutex::scoped_lock lock(mtx);
|
|
|
- if(!running)
|
|
|
+ boost::mutex::scoped_lock lock(mutex_);
|
|
|
+ if(!running_)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
- tasks.push_back(task);
|
|
|
+ tasks_.push_back(task);
|
|
|
}
|
|
|
VLOG(3) << "push task unlock";
|
|
|
- cond.notify_one();
|
|
|
+ cond_.notify_one();
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void thread_pool::set_num(int n)
|
|
|
{
|
|
|
- num = n;
|
|
|
+ num_ = n;
|
|
|
}
|
|
|
|
|
|
} // namespace hainan
|