Net——EventLoop类(一)

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

1. 简化版EventLoop

EventLoop.h

#ifndef MUDUO_NET_EVENTLOOP_H
#define MUDUO_NET_EVENTLOOP_H

#include <boost/noncopyable.hpp>

#include <muduo/base/CurrentThread.h>
#include <muduo/base/Thread.h>

namespace muduo
{
namespace net
{

///
/// Reactor, at most one per thread.
///
/// This is an interface class, so don't expose too much details.
class EventLoop : boost::noncopyable
{
public:
	  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 assertInLoopThread()
	  {
	    if (!isInLoopThread())
	    {
	      abortNotInLoopThread();
	    }
	  }
	  bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
	
	  static EventLoop* getEventLoopOfCurrentThread();

 private:
	  void abortNotInLoopThread();
		
      bool looping_; /* atomic */
	  const pid_t threadId_;		// 当前事件循环所属线程ID
};

}
}
#endif  // MUDUO_NET_EVENTLOOP_H

EventLoop. cc

#include <muduo/net/EventLoop.h>

#include <muduo/base/Logging.h>

#include <poll.h>

using namespace muduo;
using namespace muduo::net;

namespace
{
// 当前线程EventLoop对象指针
// 线程局部存储
__thread EventLoop* t_loopInThisThread = 0;
}

EventLoop* EventLoop::getEventLoopOfCurrentThread()
{
  return t_loopInThisThread;
}

EventLoop::EventLoop()
  : looping_(false),
    threadId_(CurrentThread::tid())
{
  LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
  // 如果当前线程已经创建了EventLoop对象,终止(LOG_FATAL)
  if (t_loopInThisThread)
  {
    LOG_FATAL << "Another EventLoop " << t_loopInThisThread
              << " exists in this thread " << threadId_;
  }
  else
  {
    t_loopInThisThread = this;
  }
}

EventLoop::~EventLoop()
{
  t_loopInThisThread = NULL;
}

// 事件循环,该函数不能跨线程调用
// 只能在创建该对象的线程中调用
void EventLoop::loop()
{
  assert(!looping_);
  // 断言当前处于创建该对象的线程中
  assertInLoopThread();
  looping_ = true;
  LOG_TRACE << "EventLoop " << this << " start looping";

  ::poll(NULL, 0, 5*1000);

  LOG_TRACE << "EventLoop " << this << " stop looping";
  looping_ = false;
}

void EventLoop::abortNotInLoopThread()
{
  LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
            << " was created in threadId_ = " << threadId_
            << ", current thread id = " <<  CurrentThread::tid();
}

2. 简化版测试用例 Reactor_test01.cc、Reactor_test02.cc

Reactor_test01.cc

#include <muduo/net/EventLoop.h>

#include <stdio.h>

using namespace muduo;
using namespace muduo::net;

void threadFunc()
{
	printf("threadFunc(): pid = %d, tid = %d\n",
		getpid(), CurrentThread::tid());

	EventLoop loop;
	loop.loop();
}

int main(void)
{
	printf("main(): pid = %d, tid = %d\n",
		getpid(), CurrentThread::tid());

	EventLoop loop;

	Thread t(threadFunc);
	t.start();

	loop.loop();	//主线程调用事件循环
	t.join();
	return 0;
}

运行结果如下:
在这里插入图片描述

EventLoop loop;

  • 主线程创建事件循环,EventLoop构造函数如下:
    在这里插入图片描述

Thread t(threadFunc);
t.start();

  • 创建一个子线程,线程函数是 threadFunc(),定义如下:
    在这里插入图片描述
    在线程函数内,创建了属于子线程的事件循环, loop()函数定义如下:
    在这里插入图片描述
    在loop()中,只是停顿了5s

Reactor_test02.cc

#include <muduo/net/EventLoop.h>

#include <stdio.h>

using namespace muduo;
using namespace muduo::net;

EventLoop* g_loop;

void threadFunc()
{
	g_loop->loop();
}

int main(void)
{
	EventLoop loop;
	g_loop = &loop;
	Thread t(threadFunc);
	t.start();
	t.join();
	return 0;
}

运行结果如下:
在这里插入图片描述

参考代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值