muduo_net库源码分析(五)(通过EventLoop::runInLoop实现跨线程调用)

本文深入分析muduo_net库中EventLoop的源码,探讨如何通过EventLoop::runInLoop实现线程间的通信和跨线程调用。讲解了pipe、socketpair、eventfd等线程间通知机制,并解释了EventLoop与Channel的关系,以及EventLoop::wakeup的功能。同时,文章展示了如何优化TimerQueue以支持跨线程调用的线程安全操作。
摘要由CSDN通过智能技术生成

知识点

在这里插入图片描述
pipe:等待线程关注fd[0]的可读事件,通知线程只需要往fd[1]中写入数据,fd[0]就变得可读了。
socketpair:与pipe类似,区别在于socketpair可以双向工作,pipe只能单向工作。
eventfd:比pipe更高效。
别忘了线程间的等待/通知还可以用条件变量实现。
在这里插入图片描述
一.Eventloop与Channel是聚合关系,但是有一个地方例外,这个地方就是Eventloop中等待/通知事件的Channel(即wakeupChannel_),与Eventloop是组合关系。
二.EventLoop::wakeup函数中产生了等待/通知读事件,使文件描述符wakeupFd_变得可读:
在这里插入图片描述
三.EventLoop的构造函数:
在这里插入图片描述

四.使用EventLoop::wakeup的原因
唤醒当前IO线程,因为当前线程有可能正阻塞在EventLoop::loop函数中的::poll或::epoll_wait上;
当调用EventLoop::wakeup后,马上产生了等待/通知读事件,这样就会使::poll或::epoll_wait立刻返回

在这里插入图片描述
在这里插入图片描述
对上面两张图的理解要结合EventLoop::loop函数:
在这里插入图片描述
当到来的IO事件处理完毕后,调用EventLoop::doPendingFunctors,这样设计是为了更加灵活,让IO线程也能处理一些计算任务。而EventLoop::runInLoop就是对外的公共接口函数,可跨线程调用,用来向IO线程布置计算任务,具体的任务执行仍然由IO线程完成,因此EventLoop::runInLoop不使用锁就实现了线程安全。
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

源码

首先,结合本节所学内容,对TimerQueue.cc进行如下优化,这样TimerQueue::addTimer和TimerQueue::cancel就支持跨线程调用了,且线程安全,原因如下:
在muduo_net库源码分析(四)中,TimerQueue::addTimer中直接调用了TimerQueue::addTimerInLoop,而TimerQueue::addTimerInLoop不能跨线程调用,只能在当前IO线程中调用(再次强调,每个线程至多一个EventLoop对象,创建了EventLoop对象的线程就是一个IO线程),导致TimerQueue::addTimer也无法跨线程调用;在本节中,由于使用EventLoop::runInLoop给IO线程布置计算任务(任务函数为TimerQueue::addTimerInLoop),而EventLoop::runInLoop是支持跨线程调用的,且线程安全,所以TimerQueue::addTimer就支持跨线程调用了,且线程安全
在这里插入图片描述

EventLoop.h

#ifndef MUDUO_NET_EVENTLOOP_H
#define MUDUO_NET_EVENTLOOP_H

#include <vector>

#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>

#include <muduo/base/Mutex.h>
#include <muduo/base/Thread.h>
#include <muduo/base/Timestamp.h>
#include <muduo/net/Callbacks.h>
#include <muduo/net/TimerId.h>

namespace muduo
{
   
namespace net
{
   

class Channel;
class Poller;
class TimerQueue;
///
/// Reactor, at most one per thread.
///
/// This is an interface class, so don't expose too much details.
class EventLoop : boost::noncopyable
{
   
 public:
  typedef boost::function<void()> Functor;

  EventLoop();
  ~EventLoop();  // force out-line dtor, for scoped_ptr members.

  ///
  /// Loops forever.
  ///
  /// Must be called in the same thread as creation of the object.
  ///
  void loop();

  void quit();

  ///
  /// Time when poll returns, usually means data arrivial.
  ///
  Timestamp pollReturnTime() const {
    return pollReturnTime_; }

  /// Runs callback immediately in the loop thread.
  /// It wakes up the loop, and run the cb.
  /// If in the same loop thread, cb is run within the function.
  /// Safe to call from other threads.
  void runInLoop(const Functor& cb);
  /// Queues callback in the loop thread.
  /// Runs after finish pooling.
  /// Safe to call from other threads.
  void queueInLoop(const Functor& cb);

  // timers

  ///
  /// Runs callback at 'time'.
  /// Safe to call from other threads.
  ///
  TimerId runAt(const Timestamp& time, const TimerCallback& cb);
  ///
  /// Runs callback after @c delay seconds.
  /// Safe to call from other threads.
  ///
  TimerId runAfter(double delay, const TimerCallback& cb);
  ///
  /// Runs callback every @c interval seconds.
  /// Safe to call from other threads.
  ///
  TimerId runEvery(double interval, const TimerCallback& cb);
  ///
  /// Cancels the timer.
  /// Safe to call from other threads.
  ///
  void cancel(TimerId timerId);

  // internal usage
  void wakeup();
  void updateChannel(Channel* channel);		// 在Poller中添加或者更新通道
  void removeChannel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值