|
|
@@ -4,21 +4,12 @@
|
|
|
namespace Egametang {
|
|
|
|
|
|
ThreadPool::ThreadPool(int num) :
|
|
|
- thread_num_(num), running_(false), work_num_(0)
|
|
|
+ thread_num_(num), running_(true), work_num_(0)
|
|
|
{
|
|
|
if (num == 0)
|
|
|
{
|
|
|
thread_num_ = boost::thread::hardware_concurrency();
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-ThreadPool::~ThreadPool()
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-void ThreadPool::Start()
|
|
|
-{
|
|
|
- running_ = true;
|
|
|
for (int i = 0; i < thread_num_; ++i)
|
|
|
{
|
|
|
ThreadPtr t(new boost::thread(
|
|
|
@@ -29,46 +20,39 @@ void ThreadPool::Start()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void ThreadPool::Stop()
|
|
|
+ThreadPool::~ThreadPool()
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+void ThreadPool::Wait()
|
|
|
{
|
|
|
- VLOG(3)<< "Stop";
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void ThreadPool::Runner()
|
|
|
{
|
|
|
- VLOG(3) << "thread start";
|
|
|
bool continued = true;
|
|
|
while (continued)
|
|
|
{
|
|
|
boost::function<void (void)> task;
|
|
|
{
|
|
|
- VLOG(3) << "loop lock";
|
|
|
boost::mutex::scoped_lock lock(mutex_);
|
|
|
- VLOG(3) << "loop lock ok";
|
|
|
while (running_ && tasks_.empty())
|
|
|
{
|
|
|
cond_.wait(lock);
|
|
|
- VLOG(3) << "cond";
|
|
|
}
|
|
|
if (!tasks_.empty())
|
|
|
{
|
|
|
- VLOG(3) << "fetch task";
|
|
|
task = tasks_.front();
|
|
|
tasks_.pop_front();
|
|
|
}
|
|
|
continued = running_ || !tasks_.empty();
|
|
|
- VLOG(3) << "continued = " << continued
|
|
|
- << " running = " << running_
|
|
|
- << " tasks size = " << tasks_.size();
|
|
|
- VLOG(3) << "loop unlock";
|
|
|
}
|
|
|
|
|
|
if (task)
|
|
|
@@ -78,14 +62,12 @@ void ThreadPool::Runner()
|
|
|
}
|
|
|
if (--work_num_ == 0)
|
|
|
{
|
|
|
- VLOG(3) << "work_num = " << work_num_;
|
|
|
done_.notify_one();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
bool ThreadPool::PushTask(boost::function<void (void)> task)
|
|
|
{
|
|
|
- VLOG(3) << "push task";
|
|
|
{
|
|
|
boost::mutex::scoped_lock lock(mutex_);
|
|
|
if (!running_)
|
|
|
@@ -94,7 +76,6 @@ bool ThreadPool::PushTask(boost::function<void (void)> task)
|
|
|
}
|
|
|
tasks_.push_back(task);
|
|
|
}
|
|
|
- VLOG(3) << "push task unlock";
|
|
|
cond_.notify_one();
|
|
|
return true;
|
|
|
}
|