muduo TcpServer 类;
one loop per thread 实现多线程TcpServer是在新建Connection时从event loop poor里挑选一个loop 给TcpConnection用,也就是说TcpServer 用自己的EventLoop来接受连接,
而新连接会用其他的EventLoop来执行IO
EventLoop* ioLoop = threadPool_->getNextLoop();
TcpConnectionPtr conn(new TcpConnection(ioLoop, connName,sockfd,localAddr, peerAddr)
设置线程数目
void TcpServer::setThreadNum(int numThreads)
{
assert(0 <= numThreads);
threadPool_->setThreadNum(numThreads);
}
里面涉及到一个EventLoopThreadPool类,一个thread 里面有一个EventLoop,
void EventLoopThreadPool::start(const ThreadInitCallback& cb)
{
assert(!started_);
baseLoop_->assertInLoopThread();
started_ = true;
for (int i = 0; i < numThreads_; ++i)
{
char buf[name_.size() + 32];
snprintf(buf, sizeof buf, "%s%d", name_.c_str(), i);
EventLoopThread* t = new EventLoopThread(cb, buf);
threads_.push_back(t);
loops_.push_back(t->startLoop());
}
if (numThreads_ == 0 && cb)
{
cb(baseLoop_);
}
}