muduo库阅读(36)——Net部分:事件循环线程池EventLoopThreadPool
2015-11-11 21:51
525 查看
/* * 事件循环线程池 * 这个类适用于一个进程中存在多个Reactor实例的情况 */ namespace muduo { namespace net { class EventLoop; class EventLoopThread; class EventLoopThreadPool : boost::noncopyable { public: typedef boost::function<void(EventLoop*)> ThreadInitCallback; EventLoopThreadPool(EventLoop* baseLoop, const string& nameArg); ~EventLoopThreadPool(); // 设置线程池的线程数量 void setThreadNum(int numThreads) { numThreads_ = numThreads; } // 启动线程池 void start(const ThreadInitCallback& cb = ThreadInitCallback()); // 获取下一个EventLoop对象 EventLoop* getNextLoop(); // 根据一个哈希码返回一个EventLoop对象 EventLoop* getLoopForHash(size_t hashCode); // 获取所有的EventLoop对象 std::vector<EventLoop*> getAllLoops(); // 判断线程池是否已经启动 bool started() const { return started_; } // 线程池的名字 const string& name() const { return name_; } private: //主要的EventLoop对象 EventLoop* baseLoop_; // 线程池的名字 string name_; // 线程池是否已经启动 bool started_; // 线程池中线程的数量 int numThreads_; int next_; // 线程列表 boost::ptr_vector<EventLoopThread> threads_; // EventLoop对象列表 std::vector<EventLoop*> loops_; }; } }
using namespace muduo; using namespace muduo::net; /* * 构造函数 */ EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop, const string& nameArg) : baseLoop_(baseLoop), name_(nameArg), 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 < 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_); } } // 获取下一个EventLoop对象 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; } // 根据哈希码获取一个EventLoop对象,其实哈希码就是EventLoop数组的下标 EventLoop* EventLoopThreadPool::getLoopForHash(size_t hashCode) { baseLoop_->assertInLoopThread(); EventLoop* loop = baseLoop_; if (!loops_.empty()) { loop = loops_[hashCode % loops_.size()]; } return loop; } // 获取所有的EventLoop对象 std::vector<EventLoop*> EventLoopThreadPool::getAllLoops() { baseLoop_->assertInLoopThread(); assert(started_); if (loops_.empty()) { return std::vector<EventLoop*>(1, baseLoop_); } else { return loops_; } }
相关文章推荐
- nginx+tomcat集群高可用
- linux route命令的使用详解
- Linux下安装PHP
- linux——Shell的控制结构(附shell编写代码和运行结果)
- muduo库阅读(35)——Net部分:用于执行事件循环(EventLoop)的线程类EventLoopThread
- CentOS6.5 heartbeat高可用集群的详解及工作流程
- 遥感影像序列生成视频
- Top 和 LIMIT
- Linux终端bash美化教程
- VS2013+opencv2.4.11
- 学习Opencv的小心得
- 每天一个linux命令(53):route命令
- 适用于Ubuntu的最新版Nginx源整理
- CentOS下的C连接MySQL数据库
- Linux安装软件包过慢怎么办?
- zabbix 添加对网卡流量超出阈值的监控
- 朋友介绍的一个购物网站,非常好!!!
- Linux---Ubuntu系统安装(使用技巧)
- Nginx 中last和break 及 permanent 和 redirect 的爱恨情仇
- linux下core文件调试方法