muduo源码剖析之EventLoopThread

简介: EventLoopThread类包装了一个thread类和一个EventLoop类,(one loop per thread)是封装了一个EventLoop的独立线程。

简介

EventLoopThread类包装了一个thread类和一个EventLoop类,(one loop per thread)是封装了一个EventLoop的独立线程

主要成员及属性解析

意为I/O线程类,EventLoopThread可以创建一个IO线程,通过startLoop返回一个IO线程的loop,threadFunc中开启loop循环,

源码剖析

这个类比较简单,代码都写了注释,不多说

EventLoopThread.h

#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"

namespace muduo
{
   
namespace net
{
   

class EventLoop;

class EventLoopThread : noncopyable
{
   
 public:
  typedef std::function<void(EventLoop*)> ThreadInitCallback;

  EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback(),
                  const string& name = string());
  ~EventLoopThread();
  EventLoop* startLoop();//通过此接口启动io线程,返回subloop

 private:
  void threadFunc();//线程调用函数,进入loop

  EventLoop* loop_ GUARDED_BY(mutex_);//subloop
  bool exiting_;//loop退出标志
  Thread thread_;
  MutexLock mutex_;
  Condition cond_ GUARDED_BY(mutex_);
  ThreadInitCallback callback_;//线程初始化时调用的函数,不设置则不会调用
};

}  // namespace net
}  // namespace muduo

#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"

using namespace muduo;
using namespace muduo::net;

EventLoopThread::EventLoopThread(const ThreadInitCallback& cb,
                                 const string& name)
  : loop_(NULL),      //loop未启动为NULL
    exiting_(false),    //
    thread_(std::bind(&EventLoopThread::threadFunc, this), name),   //绑定线程运行函数
    mutex_(),
    cond_(mutex_),
    callback_(cb)
{
   
}

EventLoopThread::~EventLoopThread()
{
   
  exiting_ = true;
  if (loop_ != NULL) // not 100% race-free, eg. threadFunc could be running callback_.
  {
   
    // still a tiny chance to call destructed object, if threadFunc exits just now.
    // but when EventLoopThread destructs, usually programming is exiting anyway.
    loop_->quit();
    thread_.join();   //等待线程退出
  }
}

EventLoop* EventLoopThread::startLoop()
{
   
  assert(!thread_.started());
  thread_.start(); //调用pthread_create创建线程,此时有两个线程在运行
  //一个是调用EventLoopThread::startLoop()的线程,一个是执行EventLoopThread::threadFunc()的线程(IO线程)

  EventLoop* loop = NULL;
  {
   
    MutexLockGuard lock(mutex_);
    while (loop_ == NULL)
    {
   
      cond_.wait();     //须要等待EventLoop对象的创建
    }
    loop = loop_;       //IO线程创建loop_赋给主线程
  }

  return loop;          //主线程返回IO线程创建的EventLoop对象
}

void EventLoopThread::threadFunc()            //创建线程时会调用这个函数
{
   
  EventLoop loop;       //IO线程也要创建EventLoop对象,还要通知主线程已经创建完毕

  if (callback_)//如果设置了cb则diao'y
  {
   
    callback_(&loop);     //将定义好的loop传入回调
  }

  {
   
    MutexLockGuard lock(mutex_);
    // loop_指针指向了一个栈上的对象,threadFunc函数退出之后。这个指针就失效了
    // threadFunc函数退出,就意味着线程退出了,EventLoopThread对象也就没有存在的价值了
    // 因而不会有什么大的问题
    loop_ = &loop;
    cond_.notify();     //创建好,发送通知
  }

  loop.loop();          // 会在这里循环,直到EventLoopThread析构。此后不再使用loop_訪问EventLoop了
  //assert(exiting_);
  MutexLockGuard lock(mutex_);
  loop_ = NULL;
}
目录
相关文章
|
4月前
|
API
muduo源码剖析之SocketOps类
对socket设置API的封装。比较简单,已经编写注释。
17 0
|
4月前
muduo源码剖析之Socket类
封装了一个sockfd相关的设置。比较简单,已经编写注释。
17 0
|
4月前
muduo源码剖析之InetAddress
InetAddress 类在 muduo 网络库中被广泛使用,用于表示网络中的通信实体的地址信息,例如服务器地址、客户端地址等。通过 InetAddress 类,我们可以方便地操作 IP 地址和端口号,实现网络通信的功能。InetAddress 类是 muduo 网络库中的一个重要类,用于表示网络中的 IP 地址和端口号。源码比较简单,已经编写详细注释。
35 0
|
4月前
|
安全 API
muduo源码剖析之EventLoop事件循环类
EventLoop.cc就相当于一个reactor,多线程之间的函数调用(用eventfd唤醒),epoll处理,超时队列处理,对channel的处理。运行loop的进程被称为IO线程,EventLoop提供了一些API确保相应函数在IO线程中调用,确保没有用互斥量保护的变量只能在IO线程中使用,也封装了超时队列的基本操作。
41 0
|
4月前
muduo源码剖析之Acceptor监听类
Acceptor类用于创建套接字,设置套接字选项,调用socket()->bind()->listen()->accept()函数,接受连接,然后调用TcpServer设置的connect事件的回调。listen()//在TcpServer::start中调用封装了一个listen fd相关的操作,用于mainLoop接受器封装,实质上就是对Channel的多一层封装监听连接 当新连接进入时,调用Socket::accept创建套接字,触发TcpServer的回调TcpServer通过该接口设置回调,
24 0
|
4月前
|
前端开发
muduo源码剖析之AsyncLogging异步日志类
AsyncLogging是muduo的日志,程序如果直接让文件写日志可能会发生阻塞,muduo前端设计了2个BufferPtr,分别是currentBuffer_和nextBuffer_,还有一个存放BufferPtr的vector(buffers_)。多个前端线程往currentBuffer_写数据,currentBuffer_写满了将其放入buffers_,通知后端线程读。前端线程将currentBuffer_和nextBuffer_替换继续写currentBuffer_。
27 0
|
4月前
|
Java
muduo源码剖析之EventLoopThreadPool
EventLoopThreadPool是EventLoopThread类的线程池类封装了若干个EventLoopThread的线程池,所有者是一个外部的EventLoop。
19 0
|
4月前
|
安全 C++ 容器
muduo源码剖析之TimerQueue类
通过timerfd实现的定时器功能,为EventLoop扩展了一系列runAt,runEvery,runEvery等函数TimerQueue中通过std::set维护所有的Timer,也可以使用优先队列实现muduo的TimerQueue是基于timerfd_create实现,这样超时很容易和epoll结合起来。等待超时事件保存在set集合中,注意set集合的有序性,从小到大排列,整个对TimerQueue的处理也就是对set集合的操作。
18 0
muduo源码剖析之TimerQueue类
|
4月前
|
API
muduo源码剖析之poller/EpollPoller多路复用类
poller是I/O多路复用接口抽象虚基类,对I/O多路复用API的封装,muduo提供了EPollPoller和PollPoller派生类(epoll和poll),所以不支持select.newDefaultPoller()默认选择epoll。
38 0
|
4月前
|
网络协议
muduo源码剖析之TcpClient客户端类
muduo用TcpClient发起连接,TcpClient有一个Connector连接器,TCPClient使用Conneccor发起连接, 连接建立成功后, 用socket创建TcpConnection来管理连接, 每个TcpClient class只管理一个TcpConnecction,连接建立成功后设置相应的回调函数。很显然,TcpClient用来管理客户端连接,真正连接交给Connector。
28 0
muduo源码剖析之TcpClient客户端类