服务器的启动
创建一个TcpServer(包含EevntLoop,acceptor,EventThreadPool),调用setThreadNum设立线程的数目,然后调用Start()启动server。在start()中,线程池的创建由函数threadPool_->start(threadInitCallback_)完成;启动监听由loop_->runInLoop(boost::bind(&Acceptor::listen, get_pointer(acceptor_)))完成
线程池的创建
threadPool_->start(threadInitCallback_)的实现如下:
void EventLoopThreadPool::start(const ThreadInitCallback& cb)
{
assert(!started_);
baseLoop_->assertInLoopThread();
started_ = true;
for (int i = 0; i < numThreads_; ++i)
{
EventLoopThread* t = new EventLoopThread(cb);//new 一个 thread
threads_.push_back(t);
loops_.push_back(t->startLoop());
}
if (numThreads_ == 0 && cb)
{
cb(baseLoop_);
}
}
EventLoopThread中包含一个Thread变量,其构造函数如下: