EventLoopThread类图
EventLoopThread类
- EventLoopThread体现了muduo库的思想:每一个线程有且有一个eventloop对象。此类的功能也是,创建一个线程,并在线程的回调函数中创造一个eventloop对象
- EventLoopThread是线程池中分配的线程类,内部是使用std::thread,每个线程持有一个自己的EventLoop。
数据成员:
EventLoop* loop_:loop_指针指向一个EventLoop对象
bool exiting_:线程是否退出
Thread thread_:线程对象thread_
MutexLock mutex_:互斥量对象mutex_
Condition cond_:已经创建的线程的个数,原子整数类
ThreadInitCallback callback_:回调函数callback_,回调函数在EventLoop::loop事件循环之前被调用
typedef
typedef boost::function<void(EventLoop*)> ThreadInitCallback
成员函数:
EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback()):构造函数,需要传递一个回调函数
~EventLoopThread():析构函数
EventLoop* startLoop():启动线程,该线程就成为了IO线程。这个函数需要子线程先产生eventloop对象,主线程才能返回这个eventloop对象的指针。由于这里要求线程之间的先后顺序,于是这里就借助了条件变量来完成。
void threadFunc():线程函数,在线程thread_启动时调用
EventLoopThread.h
// 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 a public header file, it must only include public header files.
//EventLoopThread类,该类封装了IO线程
#ifndef MUDUO_NET_EVENTLOOPTHREAD_H
#define MUDUO_NET_EVENTLOOPTHREAD_H
#include <muduo/base/Condition.h>
#include <muduo/base/Mutex.h>
#include <muduo/base/Thread.h>
#include <boost/noncopyable.hpp>
namespace muduo
{
namespace net
{
class EventLoop;
class EventLoopThread : boost::noncopyable
{
public:
typedef boost::function<void(EventLoop*)> ThreadInitCallback;
//构造函数需要传递一个回调函数
EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback());
~EventLoopThread();
//启动线程,该线程就成为了IO线程
EventLoop* startLoop();
private:
//线程函数,在线程thread_启动时调用
void threadFunc();
//loop_指针指向一个EventLoop对象
EventLoop* loop_;
//线程是否退出
bool exiting_;
//线程对象thread_
Thread thread_;
//互斥量对象mutex_
MutexLock mutex_;
//条件变量对象cond_
Condition cond_;
// 回调函数callback_,回调函数在EventLoop::loop事件循环之前被调用
ThreadInitCallback callback_;
};
}
}
#endif // MUDUO_NET_EVENTLOOPTHREAD_H
EventLoopThread.cc
// 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/EventLoopThread.h>
#include <muduo/net/EventLoop.h>
#include <boost/bind.hpp>
using namespace muduo;
using namespace muduo::net;
EventLoopThread::EventLoopThread(const ThreadInitCallback& cb)
: loop_(NULL),
exiting_(false),
thread_(boost::bind(&EventLoopThread::threadFunc, this)),
mutex_(),
cond_(mutex_),
callback_(cb)
{
}
EventLoopThread::~EventLoopThread()
{
exiting_ = true;
//退出IO线程,让IO线程的loop循环退出,从而退出了IO线程
loop_->quit();
thread_.join();
}
EventLoop* EventLoopThread::startLoop()
{
assert(!thread_.started());
thread_.start();
{
MutexLockGuard lock(mutex_);
while (loop_ == NULL)
{
cond_.wait();
}
}
return loop_;
}
void EventLoopThread::threadFunc()
{
EventLoop loop;
if (callback_)
{
callback_(&loop);
}
{
MutexLockGuard lock(mutex_);
// loop_指针指向了一个栈上的对象,threadFunc函数退出之后,这个指针就失效了
// threadFunc函数退出,就意味着线程退出了,EventLoopThread对象也就没有存在的价值了。
// 因而不会有什么大的问题
loop_ = &loop;
cond_.notify();
}
loop.loop();
//assert(exiting_);
}