知识点
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