ThreadPool
1 构造函数和析构函数的实现
ThreadPool::ThreadPool(const string& nameArg)函数的实现
ThreadPool::ThreadPool(const string& nameArg)
: mutex_(),
notEmpty_(mutex_),
notFull_(mutex_),
name_(nameArg),
maxQueueSize_(0),
running_(false)
{
}
ThreadPool::~ThreadPool()
ThreadPool::~ThreadPool()
{
//如果线程池正在运行
if (running_)
{
//停止运行
stop();
}
}
2 成员函数的实现
void setMaxQueueSize(int maxSize)函数的实现
void setMaxQueueSize(int maxSize) { maxQueueSize_ = maxSize; }
void setThreadInitCallback(const Task& cb)函数的实现
void setThreadInitCallback(const Task& cb)
{ threadInitCallback_ = cb; }
void setThreadInitCallback(const Task& cb)函数的实现
void ThreadPool::start(int numThreads)
{
assert(threads_.empty());
//设置running为true
running_ = true;
//预留线程池中线程数量
threads_.reserve(numThreads);
for (int i = 0; i < numThreads; ++i)
{
char id[32];
snprintf(id, sizeof id, "%d", i+1);
//创建numThreads数量的子线程
//其中线程执行的任务函数为runInThread
threads_.emplace_back(new muduo::Thread(
std::bind(&ThreadPool::runInThread, this), name_+id));
threads_[i]->start();
}
if (numThreads == 0 && threadInitCallback_)
{
threadInitCallback_();
}
}
void stop()函数的实现
void ThreadPool::stop()
{
{
MutexLockGuard lock(mutex_);
//设置running_为false
running_ = false;
//通知所有的线程
notEmpty_.notifyAll();
}
for (auto& thr : threads_)
{
thr->join();
}
}
const string& name() const函数的实现
const string& name() const
//返回线程池的名字
{ return name_; }
** size_t queueSize() const函数的实现**
size_t ThreadPool::queueSize() const
{
MutexLockGuard lock(mutex_);
return queue_.size();
}
void run(Task f)函数的实现
void ThreadPool::run(Task task)
{
//如果说没有子线程,则自己执行该任务函数
if (threads_.empty())
{
task();
}
else
{
//否则,如果任务队列满了,则阻塞等待
MutexLockGuard lock(mutex_);
while (isFull())
{
notFull_.wait();
}
assert(!isFull());
//任务队列中添加该任务函数
queue_.push_back(std::move(task));
//通知所有的线程
notEmpty_.notify();
}
}
bool isFull() const 函数的实现
bool ThreadPool::isFull() const
{
mutex_.assertLocked();
//判断队列的大小是否大于等于最大容量
return maxQueueSize_ > 0 && queue_.size() >= maxQueueSize_;
}
void runInThread()函数的实现
void ThreadPool::runInThread()
{
try
{
if (threadInitCallback_)
{
threadInitCallback_();
}
//如果线程池正在运行
while (running_)
{
//从任务队列中取出任务
Task task(take());
if (task)
{
//如果task不为空,执行该任务
task();
}
}
}
catch (const Exception& ex)
{
fprintf(stderr, "exception caught in ThreadPool %s\n", name_.c_str());
fprintf(stderr, "reason: %s\n", ex.what());
fprintf(stderr, "stack trace: %s\n", ex.stackTrace());
abort();
}
catch (const std::exception& ex)
{
fprintf(stderr, "exception caught in ThreadPool %s\n", name_.c_str());
fprintf(stderr, "reason: %s\n", ex.what());
abort();
}
catch (...)
{
fprintf(stderr, "unknown exception caught in ThreadPool %s\n", name_.c_str());
throw; // rethrow
}
}
Task take()函数的实现
ThreadPool::Task ThreadPool::take()
{
MutexLockGuard lock(mutex_);
// always use a while-loop, due to spurious wakeup
//当任务队列为空,或者running为false,阻塞等待
while (queue_.empty() && running_)
{
notEmpty_.wait();
}
Task task;
if (!queue_.empty())
{
//去除任务队列的第一个任务
//删除任务队列中的第一个任务
task = queue_.front();
queue_.pop_front();
if (maxQueueSize_ > 0)
{
notFull_.notify();
}
}
//返回取出的任务
return task;
}