muduo¿âÈçºÎÖ§³Ö¶àÏß³Ì
EventLoopThread£¨IOÏß³ÌÀࣩ
EventLoopThreadPool£¨IOÏ̳߳ØÀࣩ
IOÏ̳߳صŦÄÜÊÇ¿ªÆôÈô¸É¸öIOỊ̈߳¬²¢ÈÃÕâЩIOÏ̴߳¦ÓÚʼþÑ»·µÄ״̬
ÏÂÃæµÄÕâЩ´úÂë¿ÉÄܺÍÇ°Ãæ¸ø³öµÄÔ´´úÂëÓÐЩ²»Ò»Ñù£¬ÔĶÁµÄͬѧÇë×¢ÒâÁË
EventLoopThreadPoolÍ·Îļþ
eventloopthreadpool.h
[cpp
// Copyright 2010, Shuo Chen. All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
// Author: Shuo Chen £¨chenshuo at chenshuo dot com£©
//
// This is an internal header file, you should not include this.
#ifndef MUDUO_NET_EVENTLOOPTHREADPOOL_H
#define MUDUO_NET_EVENTLOOPTHREADPOOL_H
#include <muduo/base/Condition.h>
#include <muduo/base/Mutex.h>
#include <vector>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
namespace muduo
{
namespace net
{
class EventLoop;
class EventLoopThread;
class EventLoopThreadPool : boost::noncopyable
{
public:
typedef boost::function<void£¨EventLoop*£©> 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<EventLoopThread> threads_; // IOÏß³ÌÁбí
std::vector<EventLoop*> loops_; // EventLoopÁбí
};
}
}
#endif // MUDUO_NET_EVENTLOOPTHREADPOOL_H
EventLoopThreadPoolÔ´Îļþ
eventloopthreadpool.cc
[cpp]
// Copyright 2010, Shuo Chen. All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
// Author: Shuo Chen £¨chenshuo at chenshuo dot com£©
#include <muduo/net/EventLoopThreadPool.h>
#include <muduo/net/EventLoop.h>
#include <muduo/net/EventLoopThread.h>
#include <boost/bind.hpp>
using namespace muduo;
using namespace muduo::net;
EventLoopThreadPool::EventLoopThreadPool£¨EventLoop* baseLoop£©
: baseLoop_£¨baseLoop£©£¬ // ÓëAcceptorËùÊôEventLoopÏàͬ
started_£¨false£©£¬
numThreads_£¨0£©£¬
next_£¨0£©
{
}
EventLoopThreadPool::~EventLoopThreadPool£¨£©
{
// Don't delete loop, it's stack variable
}