| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #include "Thread/ThreadPool.h"
- namespace Egametang {
- ThreadPool::ThreadPool(int num) :
- threadNum(num), running(true), workNum(0)
- {
- if (num == 0)
- {
- threadNum = boost::thread::hardware_concurrency();
- }
- for (int i = 0; i < threadNum; ++i)
- {
- ThreadPtr t(new boost::thread(
- boost::bind(&ThreadPool::Runner, this)));
- threads.push_back(t);
- t->detach();
- ++workNum;
- }
- }
- ThreadPool::~ThreadPool()
- {
- }
- void ThreadPool::Wait()
- {
- boost::mutex::scoped_lock lock(mutex);
- running = false;
- cond.notify_all();
- while (workNum > 0)
- {
- done.wait(lock);
- }
- }
- void ThreadPool::Runner()
- {
- bool continued = true;
- while (continued)
- {
- boost::function<void (void)> task;
- {
- boost::mutex::scoped_lock lock(mutex);
- while (running && tasks.empty())
- {
- cond.wait(lock);
- }
- if (!tasks.empty())
- {
- task = tasks.front();
- tasks.pop_front();
- }
- continued = running || !tasks.empty();
- }
- if (task)
- {
- task();
- }
- }
- if (--workNum == 0)
- {
- done.notify_one();
- }
- }
- bool ThreadPool::Schedule(boost::function<void (void)> task)
- {
- {
- boost::mutex::scoped_lock lock(mutex);
- if (!running)
- {
- return false;
- }
- tasks.push_back(task);
- }
- cond.notify_one();
- return true;
- }
- } // namespace Egametang
|