|
@@ -17,7 +17,7 @@ namespace hainan
|
|
|
running = true;
|
|
running = true;
|
|
|
for(int i = 0; i < num; ++i)
|
|
for(int i = 0; i < num; ++i)
|
|
|
{
|
|
{
|
|
|
- shared_ptr<thread> t(new thread(bind(&ThreadPool::Loop, ref(this))));
|
|
|
|
|
|
|
+ shared_ptr<thread> t(new thread(bind(&ThreadPool::Runner, this)));
|
|
|
threads.push_back(t);
|
|
threads.push_back(t);
|
|
|
t->detach();
|
|
t->detach();
|
|
|
}
|
|
}
|
|
@@ -37,21 +37,22 @@ namespace hainan
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void ThreadPool::Loop()
|
|
|
|
|
|
|
+ void ThreadPool::Runner()
|
|
|
{
|
|
{
|
|
|
VLOG(2) << "thread start";
|
|
VLOG(2) << "thread start";
|
|
|
bool continued = true;
|
|
bool continued = true;
|
|
|
while(continued)
|
|
while(continued)
|
|
|
{
|
|
{
|
|
|
- function<void (void)> task(0);
|
|
|
|
|
|
|
+ function<void (void)> task;
|
|
|
{
|
|
{
|
|
|
|
|
+ VLOG(2) << "loop lock";
|
|
|
mutex::scoped_lock lock(mtx);
|
|
mutex::scoped_lock lock(mtx);
|
|
|
- if(running && tasks.empty())
|
|
|
|
|
|
|
+ VLOG(2) << "loop lock ok";
|
|
|
|
|
+ while(running && tasks.empty())
|
|
|
{
|
|
{
|
|
|
cond.wait(lock);
|
|
cond.wait(lock);
|
|
|
- VLOG(2) << "get cond";
|
|
|
|
|
|
|
+ VLOG(2) << "cond";
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
if(!tasks.empty())
|
|
if(!tasks.empty())
|
|
|
{
|
|
{
|
|
|
VLOG(2) << "fetch task";
|
|
VLOG(2) << "fetch task";
|
|
@@ -59,7 +60,10 @@ namespace hainan
|
|
|
tasks.pop_front();
|
|
tasks.pop_front();
|
|
|
}
|
|
}
|
|
|
continued = running || !tasks.empty();
|
|
continued = running || !tasks.empty();
|
|
|
- VLOG(2) << "running = " << running;
|
|
|
|
|
|
|
+ VLOG(2) << "continued = " << continued
|
|
|
|
|
+ << "running = " << running
|
|
|
|
|
+ << " tasks size = " << tasks.size();
|
|
|
|
|
+ VLOG(2) << "loop unlock";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if(task)
|
|
if(task)
|
|
@@ -85,11 +89,12 @@ namespace hainan
|
|
|
}
|
|
}
|
|
|
tasks.push_back(task);
|
|
tasks.push_back(task);
|
|
|
}
|
|
}
|
|
|
|
|
+ VLOG(2) << "push task unlock";
|
|
|
cond.notify_one();
|
|
cond.notify_one();
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void ThreadPool::SetNum(int32_t n)
|
|
|
|
|
|
|
+ void ThreadPool::SetNum(int n)
|
|
|
{
|
|
{
|
|
|
num = n;
|
|
num = n;
|
|
|
}
|
|
}
|