Muduo学习笔记(一) 什么都不做的EventLoop
Muduo学习笔记(一) 什么都不做的EventLoop
EventLoop
EventLoop的基本接口包括构造、析构、loop()。
One Loop Per Thread 一个线程只有一个EventLoop对象、如果当前线程创建了其他 EventLoop对象,则终止程序.
CurrentThread
CurrentThread 通过__thread 关键字和系统调用syscall() 保存获取线程的的pid(不通于线程tid,tid属于进程,进程内唯一,线程pid属于内核).
#ifndef _CURRENT_THREAD
#define _CURRENT_THREAD
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <unistd.h>
namespace CurrentThread
{
// internal
extern __thread int t_cachedTid;
extern __thread char t_tidString[32];
extern __thread int t_tidStringLength;
extern __thread const char* t_threadName;
inline int tid()
{
if (__builtin_expect(t_cachedTid == 0, 0))
{
if (t_cachedTid == 0)
{
t_cachedTid = static_cast<pid_t>(::syscall(SYS_gettid));
t_tidStringLength = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);
}
}
return t_cachedTid;
}
inline const char* tidString() // for logging
{
return t_tidString;
}
inline int tidStringLength() // for logging
{
return t_tidStringLength;
}
inline const char* name()
{
return t_threadName;
}
}
#endif
//CurrentThread.cpp
#include "CurrentThread.hh"
namespace CurrentThread
{
__thread int t_cachedTid = 0;
__thread char t_tidString[32];
__thread int t_tidStringLength = 6;
__thread const char* t_threadName = "unknown";
}
getEventLoopOfCurrentThread
每个线程至多有一个EventLoop对象,那么我们通过static 成员函数getEventLoopOfCurrentThread() 返回此对象.
EventLoop* EventLoop::getEventLoopOfCurrentThread()
{
return t_loopInThisThread;
}
EventLoop 源代码
#ifndef NET_EVENTLOOP_H
#define NET_EVENTLOOP_H
#include "CurrentThread.hh"
class EventLoop
{
public:
EventLoop();
~EventLoop();
void loop();
void assertInLoopThread()
{
if(!isInloopThread())
{
abortNotInLoopThread();
}
}
bool isInloopThread() const {return m_threadId == CurrentThread::tid(); }
static EventLoop* getEventLoopOfCurrentThread();
private:
EventLoop& operator=(const EventLoop&);
EventLoop(const EventLoop&);
void abortNotInLoopThread();
bool m_looping;
const pid_t m_threadId;
};
#endif
// EventLoop.cpp
#include "EventLoop.hh"
#include "Logger.hh"
#include <assert.h>
#include <poll.h>
__thread EventLoop* t_loopInThisThread = 0;
EventLoop::EventLoop()
:m_looping(false),
m_threadId(CurrentThread::tid())
{
LOG_TRACE << "EventLoop Create " << this << " in thread " << m_threadId;
if(t_loopInThisThread)
{ //每个线程只有一个EventLoop对象 , 如果当前线程创建了其他 EventLoop对象,则终止程序.
LOG_FATAL << "Anthor EventLoop " << t_loopInThisThread
<< " exists in this thread " << m_threadId;
}
else
{
t_loopInThisThread = this;
}
}
EventLoop::~EventLoop()
{
assert(!m_looping);
t_loopInThisThread = NULL;
}
void EventLoop::loop()
{
assert(!m_looping);
assertInLoopThread();
m_looping = true;
LOG_TRACE << "EventLoop " << this << " start loopig";
::poll(NULL, 0, 3*1000);
LOG_TRACE << "EventLoop " << this << " stop loopig";
m_looping = false;
}
void EventLoop::abortNotInLoopThread()
{
LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
<< " was created in threadId_ = " << m_threadId
<< ", current thread id = " << CurrentThread::tid();
}
EventLoop* EventLoop::getEventLoopOfCurrentThread()
{
return t_loopInThisThread;
}
测试程序
test1
正确的逻辑.
#include <errno.h>
#include "EventLoop.hh"
#include <thread>
int main()
{
EventLoop testloop;
testloop.loop();
return 0;
}
./test.out
2018-10-25 20:01:03.287601 [TRACE] [EventLoop.cpp:12] [EventLoop] EventLoop Create 0x7FFF7B1E2780 in thread 2086
2018-10-25 20:01:03.287750 [TRACE] [EventLoop.cpp:36] [loop] EventLoop 0x7FFF7B1E2780 start loopig
2018-10-25 20:01:06.291622 [TRACE] [EventLoop.cpp:40] [loop] EventLoop 0x7FFF7B1E2780 stop loopig
test2
企图在当前线程启用其他线程创建的EventLoop对象
#include <errno.h>
#include "EventLoop.hh"
#include <thread>
EventLoop* g_loop;
void test()
{
g_loop->loop();
}
int main()
{
EventLoop testloop;
//testloop.loop();
g_loop = &testloop;
std::thread test_thread(test);
test_thread.join();
return 0;
}
./test.out
2018-10-25 20:05:49.618701 [TRACE] [EventLoop.cpp:12] [EventLoop] EventLoop Create 0x7FFCA55A35F0 in thread 2114
2018-10-25 20:05:49.619057 [FATAL] [EventLoop.cpp:47] EventLoop::abortNotInLoopThread - EventLoop 0x7FFCA55A35F0 was created in threadId_ = 2114, current thread id = 2115
Aborted (core dumped)
Muduo学习笔记(一) 什么都不做的EventLoop的更多相关文章
- muduo学习笔记(二)Reactor关键结构
目录 muduo学习笔记(二)Reactor关键结构 Reactor简述 什么是Reactor Reactor模型的优缺点 poll简述 poll使用样例 muduo Reactor关键结构 Chan ...
- muduo学习笔记(六) 多线程的TcpServer
目录 前言 多线程TcpServer EventLoopThreadPool 线程池设计模式 muduo中的使用 连接的建立.消息.销毁 on_connection on_message on_clo ...
- Python学习笔记:Flask-Migrate基于model做upgrade的基本原理
1)flask-migrate的官网:https://flask-migrate.readthedocs.io/en/latest/ 2)获取帮助,在pycharm的控制台中输入 flask d ...
- Python学习笔记1——人人都爱列表
一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...
- Java学习笔记--HashMap中使用object做key的问题【转】
在HashMap中,如果需要使用多个属性组合作为key,可以将这几个属性组合成一个对象作为key.但是存在的问题是,要做get时,往往没办法保存当初put操作时的key object的referenc ...
- 学习笔记之08试用div做网页(滨院)-小作业
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C#学习笔记(35)——事件做的登录案例
说明(2018-4-9 20:11:42): 1. 先自定义了一个登录控件,可以输入账号.密码,点击登录.然后在Form1里面拖入这个控件,要求输入账号密码正确时,点击登录,控件显示绿色,否则显示红色 ...
- 自然语言处理NLP学习笔记三:使用Django做一个NLP的Web站点
前言: 前面我们已经能初步实现一个中文自然处理语言的模型了,但交互界面是命令行的,不太友好. 如果想做一个类似http://xiaosi.trs.cn/demo/rs/demo的界面,那就还需要继续往 ...
- Mudo C++网络库第六章学习笔记
muduo网络库简介 高级语言(Java, Python等)的Sockects库并没有对Sockects API提供更高层的封装, 直接用它编写程序很容易掉到陷阱中: 网络库的价值还在于能方便地处理并 ...
随机推荐
- PFX文件提取公钥私钥
jks是JAVA的keytools证书工具支持的证书私钥格式.pfx是微软支持的私钥格式. cer是证书的公钥. 如果是你私人要备份证书的话记得一定要备份成jks或者pfx格式,否则恢复不了. 简单来 ...
- Spark job 部署模式
Spark job 的部署有两种模式,Client && Cluster spark-submit .. --deploy-mode client | cluster [上传 Jar ...
- Active Directory、Exchange、单点登录,企业账号统一管理解决方案
现在的公司一般都会有很多内部管理系统,比如OA.ERP.CRM.邮件系统等.员工入职之后如果每个系统都创建一个账号和密码,首先员工记系统账号就是一件非常头疼的事情,如果公司有一百个系统那就得创建一百个 ...
- 2.1Python基础语法(一)之注释与数据类型:
返回总目录 目录: 1.注释 2.乱码 3.变量 4.数据类型 5.数据的转换 6.动态,静态和强类型,弱类型 (一)注释:(编译时是被编译器忽略的) 1.注释的分类: 2.特殊注释: (二)乱码:( ...
- Docker容器学习与分享09
Docker容器之间的相互通信 先新建两个不同的网段,就用分享08里的两个网段作为新建的网段. [root@promote ~]# docker network ls NETWORK ID NAME ...
- 团队作业8-测试与发布(beta阶段)
小组成员 [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042) 张俊逸(201421122044).陈志建(201421122040).陈金 ...
- SecureCRT Win免安装版本,简单好用
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. 这个简单好用,程序员必备. 下载地址:SecureCRT. ...
- 13.1SolrCloud集群使用手册之Collections API
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.创建collection name:指明collection名字 router.name:指定路由策略,默 ...
- 使用mysql乐观锁解决并发问题思路
本文摘自网络,仅供个人学习之用 案例说明: 银行两操作员同时操作同一账户.比如A.B操作员同时读取一余额为1000元的账户,A操作员为该账户增加100元,B操作员同时为该账户扣除50元,A先提交,B后 ...
- 为什么web3 1.0 的接口有personal_*和eth_*的,两者有什么不同
看https://github.com/ethereum/EIPs/pull/712 Why personal_* namespace instead of eth_* namespace? I be ...