[Muduo网络库源码分析] (13) net/EventLoopThread_h_c新建一个专门用于EventLoop的线程

本文介绍了一种基于muduo库实现EventLoop线程的方法,通过创建一个专用线程来运行EventLoop,确保事件循环独立运行。文章详细解析了EventLoopThread类的设计,包括其构造函数、析构函数、startLoop方法和线程函数threadFunc的实现细节。

新建一个专门用于 EventLoop 的线程

实现:启动一个线程,在其中运行 EventLoop::loop()


功能:运行Evenloop


知识点

  • 条件变量使用:当多个线程对同一个变量进行操作时使用条件变量对其进行保护,条件变量同互斥锁一同起作用。

代码及分析:

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)

#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,
                                 const string& name)
  : loop_(NULL),
    exiting_(false),
    thread_(boost::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();//退出loop
    thread_.join();//阻塞子线程
  }
}


//开始loop
EventLoop* EventLoopThread::startLoop()
{
  assert(!thread_.started());
  thread_.start();
  //因为要涉及到两个线程访问同一个变量,所以用条件变量进行保护
  //等待子线程创建完loop后再返回
  {
    MutexLockGuard lock(mutex_);
    while (loop_ == NULL)
    {
      cond_.wait();
    }
  }

  return loop_;
}

//线程函数
void EventLoopThread::threadFunc()
{
  EventLoop loop;
  //如果存在回调函数,调用回调函数对loop进行修改
  if (callback_)
  {
    callback_(&loop);
  }

  //当线程成功创建了loop就唤醒父线程返回loop
  {
    MutexLockGuard lock(mutex_);
    loop_ = &loop;
    cond_.notify();
  }
  //开始loop
  loop.loop();
  //assert(exiting_);
  loop_ = NULL;
}


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)
//
// This is a public header file, it must only include public header files.

#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(),
                  const string& name = string());
  ~EventLoopThread();
  EventLoop* startLoop();

 private:
  //线程函数
  void threadFunc();

  //EventLoop 对象
  EventLoop* loop_;

  bool exiting_;  //loop是否退出

  //线程对象 
  Thread thread_;
  MutexLock mutex_; //互斥锁
  Condition cond_;  //条件变量

  //回调函数
  ThreadInitCallback callback_;
};

}
}

#endif  // MUDUO_NET_EVENTLOOPTHREAD_H


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值