muduo库如何支持one loop per thread
由以下类实现
EventLoopThread(IO线程类)
EventLoopThreadPool(IO线程池类)
IO线程池的功能是开启若干个IO线程,并让这些IO线程处于事件循环的状态
#ifndef MUDUO_NET_EVENTLOOPTHREADPOOL_H
#define MUDUO_NET_EVENTLOOPTHREADPOOL_H#include
#include #include
#include
#include
#include namespace muduo
{namespace net
{class EventLoop;
class EventLoopThread;class EventLoopThreadPool : boost::noncopyable
{public:typedef boost::function ThreadInitCallback;EventLoopThreadPool(EventLoop* baseLoop);~EventLoopThreadPool();void setThreadNum(int numThreads) { numThreads_ = numThreads; }void start(const ThreadInitCallback& cb = ThreadInitCallback());EventLoop* getNextLoop();private:EventLoop* baseLoop_; // 与Acceptor所属EventLoop相同bool started_;int numThreads_; // 线程数int next_; // 新连接到来,所选择的EventLoop对象下标boost::ptr_vector threads_; // IO线程列表,ptr_vector 对象销毁的时候管理的对象也销毁std::vector loops_; // EventLoop列表 ,这些对象都是线程中的栈上对象,不用销毁
};}
}#endif // MUDUO_NET_EVENTLOOPTHREADPOOL_H
#include #include
#include #include using namespace muduo;
using namespace muduo::net;EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop): baseLoop_(baseLoop),started_(false),numThreads_(0),next_(0)
{
}EventLoopThreadPool::~EventLoopThreadPool()
{// Don't delete loop, it's stack variable
}void EventLoopThreadPool::start(const ThreadInitCallback& cb)
{assert(!started_);baseLoop_->assertInLoopThread();started_ = true;for (int i = 0; i startLoop()); // 启动EventLoopThread线程,在进入事件循环之前,会调用cb}if (numThreads_ == 0 && cb){// 只有一个EventLoop,在这个EventLoop进入事件循环之前,调用cbcb(baseLoop_);}
}EventLoop* EventLoopThreadPool::getNextLoop()
{baseLoop_->assertInLoopThread();EventLoop* loop = baseLoop_;// 如果loops_为空,则loop指向baseLoop_// 如果不为空,按照round-robin(RR,轮叫)的调度方式选择一个EventLoopif (!loops_.empty()){// round-robinloop = loops_[next_];++next_;if (implicit_cast(next_) >= loops_.size()){next_ = 0;}}return loop;
}