多线程TcpServer

多线程TcpServer自己的EventLoop只用来接收新连接(即TcpServer所属线程的EventLoop只监听listen fd),而新连接会用其他EventLoop来执行IO(即每个新TcpConnection对象都会在线程池中分配一个线程来处理该连接上的IO事件).

单线程TcpServer的EventLoop是与TcpConnection共享的,listen fd和accept返回的客户连接fd都是在一个EventLoop中处理的.

TcpServer每次新建一个TcpConnection就会调用getNextLoop()来取得EventLoop,如果是单线程服务,每次返回的都是baseLoop_,即TcpServer自己用的那个loop

EventLoop* EventLoopThreadPool::getNextLoop()
{
  baseLoop_->assertInLoopThread();
  assert(started_);
  EventLoop* loop = baseLoop_;

  if (!loops_.empty())
  {
    // round-robin
    loop = loops_[next_];
    ++next_;
    if (implicit_cast<size_t>(next_) >= loops_.size())
    {
      next_ = 0;
    }
  }
  return loop;
}

muduo目前的设计是每个TcpServer有自己的EventLoopThreadPool,多个TcpServer之间不共享EventLoopThreadPool.

原文地址:https://www.cnblogs.com/ljygoodgoodstudydaydayup/p/5676090.html