1// excerpts from http://code.google.com/p/muduo/ 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the License file. 5// 6// Author: Shuo Chen (chenshuo at chenshuo dot com) 7 8#include "EventLoopThreadPool.h" 9 10#include "EventLoop.h" 11#include "EventLoopThread.h" 12 13#include <boost/bind.hpp> 14 15using namespace muduo; 16 17EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop) 18 : baseLoop_(baseLoop), 19 started_(false), 20 numThreads_(0), 21 next_(0) 22{ 23} 24 25EventLoopThreadPool::~EventLoopThreadPool() 26{ 27 // Don't delete loop, it's stack variable 28} 29 30void EventLoopThreadPool::start() 31{ 32 assert(!started_); 33 baseLoop_->assertInLoopThread(); 34 35 started_ = true; 36 37 for (int i = 0; i < numThreads_; ++i) 38 { 39 EventLoopThread* t = new EventLoopThread; 40 threads_.push_back(t); 41 loops_.push_back(t->startLoop()); 42 } 43} 44 45EventLoop* EventLoopThreadPool::getNextLoop() 46{ 47 baseLoop_->assertInLoopThread(); 48 EventLoop* loop = baseLoop_; 49 50 if (!loops_.empty()) 51 { 52 // round-robin 53 loop = loops_[next_]; 54 ++next_; 55 if (static_cast<size_t>(next_) >= loops_.size()) 56 { 57 next_ = 0; 58 } 59 } 60 return loop; 61} 62 63