本篇文章在上一篇的基础上,使用 epoll  实现了一个事件监听和回调处理的模块。如何编写一个使用该模块的例子呢? 监测什么类型的fd,监测什么类型的事件,监测到事件以后需要做什么?后来可以看看如何将该模块与socket , 回调函数, 线程池联系起来。

#include<sys/epoll.h> // epoll_create, epoll_ctl, epoll_wait
#include <mutex> // std::mutex
#include <functional> // std::function
#include <iostream>
#include <memory> // std::unique_ptr
#include <unistd.h> // close class Epoll{
public:
class ActiveEvents {
public:
ActiveEvents( int num, const struct epoll_event* events):
num_( num ),
events_( events )
{
}
int num() const { return num_; }
const struct epoll_event* events() const { return events_; } private:
int num_;
const struct epoll_event* events_;
}; Epoll();
~Epoll(); int AddMonitorReadableEvent( int fd );
int AddMonitorWritableEvent( int fd );
int DeleteMonitoringEvent( int fd );
int ModifyMonitorEvent( int fd , int status ); void StartPolling();
void HandleEvents( int num, const struct epoll_event* events );
void Stop(); using EpollAwakeCallBack = std::function< void(const ActiveEvents*) > ;
void SetAwakeCallBack( EpollAwakeCallBack* cb ); // 设置事件处理回调函数 private :
int Add_Event( int fd, int event ); // 封装 epoll_ctl, 使函数语义看起来更明确
int Delete_Event( int fd, int event );
int Modify_Event( int fd, int event ); int epollfd_;
std::unique_ptr< EpollAwakeCallBack > awake_cb_ ;
static const int fd_size_; // 最多能处理的fd个数
std::mutex awake_cb_mutex_; // 由于使用std::unique_ptr保存回调函数指针,设置回调函数时需要加锁
std::mutex mutex_;
}; const int Epoll::fd_size_ = 100 ; Epoll::Epoll() {
epollfd_ = epoll_create(fd_size_);
std::cout << "epollfd = " << epollfd_ << std::endl;
} Epoll::~Epoll() {
std::cout << "deleting epoll" << std::endl;
close(epollfd_);
} void Epoll::StartPolling() {
const int EPOLLEVENTS = 100;
struct epoll_event events[EPOLLEVENTS];
while (1) {
auto ret = epoll_wait(epollfd_, events, EPOLLEVENTS, -1);
std::cout << "awakening " << ret << std::endl;
HandleEvents(ret, events);
}
} void Epoll::HandleEvents(int num, const struct epoll_event* events) {
ActiveEvents active_events(num, events);
std::unique_lock<std::mutex> lock(awake_cb_mutex_);
if (awake_cb_) {
(*awake_cb_)(&active_events);
}
} void Epoll::SetAwakeCallBack(EpollAwakeCallBack* cb) {
std::unique_lock<std::mutex> lock(awake_cb_mutex_);
awake_cb_.reset(cb);
} int Epoll::AddMonitorReadableEvent(int fd) {
// TODO: Are epoll_wait and epoll_ctl thread-safe?
std::unique_lock<std::mutex> lock(mutex_);
return Add_Event(fd, EPOLLIN | EPOLLONESHOT);
} int Epoll::AddMonitorWritableEvent(int fd) {
std::unique_lock<std::mutex> lock(mutex_);
return Add_Event(fd, EPOLLOUT | EPOLLONESHOT);
} int Epoll::DeleteMonitoringEvent(int fd) {
std::unique_lock<std::mutex> lock(mutex_);
return Delete_Event(fd, EPOLLIN | EPOLLONESHOT);
} int Epoll::ModifyMonitorEvent(int fd, int status) {
std::unique_lock<std::mutex> lock(mutex_);
return Modify_Event(fd, status);
} int Epoll::Add_Event(int fd, int event) {
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
int ret = epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev);
return ret;
} int Epoll::Delete_Event(int fd, int event) {
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
int ret = epoll_ctl(epollfd_, EPOLL_CTL_DEL, fd, &ev);
return ret;
} int Epoll::Modify_Event(int fd, int event) {
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
int ret = epoll_ctl(epollfd_, EPOLL_CTL_MOD, fd, &ev);
return ret;
} int main(){
Epoll a;
return 0;
}

Epoll的使用例子的更多相关文章

  1. 一个epoll的简单例子

    epoll事件机制的触发方式有两种:LT(电平触发)和ET(边沿触发) EPOLLIN事件: 内核中的socket接收缓冲区 为空(低电平) 内核中的socket接受缓冲区 不为空(高电平) EPOL ...

  2. Epoll简介以及例子

    第一部分:Epoll简介 问题 :  Select,Poll和Epoll的区别 答案 : Epoll和Select的区别 1. 遍历方式的区别.select判断是否有事件发生是遍历的,而epoll是事 ...

  3. epoll 系列函数简介、与select、poll 的区别

    一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...

  4. epoll学习

    一.epoll_create #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...

  5. Redis事件管理(一)

    Redis统一的时间管理器,同时管理文件事件和定时器, 这个管理器的定义: #if defined(__APPLE__) #define HAVE_TASKINFO 1 #endif /* Test ...

  6. Redis事件管理(三)

    Redis的事件管理和定时器的管理都是自己来实现的,Redis的事件管理分为两部分,一部分是封装了系统的异步事件API,还有一部分是在这基础上封装了一个通用的事件管理器,根据具体的系统来决定具体使用哪 ...

  7. epoll 简单介绍及例子

    第一部分:Epoll简介 . 当select()返回时,timeout参数的状态在不同的系统中是未定义的,因此每次调用select()之前必须重新初始化timeout和文件描述符set.实际上,秒,然 ...

  8. (转)浅析epoll – epoll例子以及分析

    原文地址:http://www.cppfans.org/1419.html 浅析epoll – epoll例子以及分析 上篇我们讲到epoll的函数和性能.这一篇用用这些个函数,给出一个最简单的epo ...

  9. epoll完整例子

    #include <deque> #include <map> #include <vector> #include <pthread.h> #incl ...

  10. epoll+socket的简单测试例子

    server: #include <sys/socket.h> #include <sys/epoll.h> #include <netinet/in.h> #in ...

随机推荐

  1. vue面试题及答案(1)

    vue面试题,知识点汇总(有答案) 一. Vue 核心小知识点 1.vue 中 key 值的作用 key     的特殊属性主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VN ...

  2. (原创)odoo中字段默认值的获取顺序

    odoo中某个字段的默认值的取值顺序 1.搜索当前记录集(recordset)中的context中是否存在"default_字段名"的键,如果存在则取值 2.搜索模型(ir.def ...

  3. py10函数之嵌套-名称空间作用域

    # 函数是第一类对象:函数名指向的值可以被当中参数传递 # 1.函数名可以被传递# name = 'jason'# x = name# print(x)# print(id(x))# def func ...

  4. scrapy中发送post请求

    1.可以使用`yield scrapy.FormRequest(url,formdata,callback)`方法发送POST请求. 其中构造参数formdata可以是字典,也可以是可迭代的(key, ...

  5. torch& tensorflow

    #torchimport torch import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def _ ...

  6. [转]B树与B+树----mysql的索引结构

    B树 和B+树是 MySQL索引使用的数据结构,对于索引优化和原理理解都非常重要,下面我的写文章就是要把B树,B+树的神秘面纱揭开,让大家在面试的时候碰到这个知识点一往无前,不再成为你的知识盲点! 1 ...

  7. 简易舰c远征计时器(HTML)

    有时候项目闲下来想休息的时候会跑跑舰c远征,但计时比较麻烦. 所以每个现场都会写那么几个工具.以前是vba的,这次写了个HTML的,感觉还算易用就仍上来了.省得以后换现场还得再写. 纯粹是偷懒时做的, ...

  8. Vmware workstation虚拟机导入到esxi虚拟机

    VMware Workstation 与 ESXi 的主要区别 VMware Workstation是直接在windows系统下安装软件,安装后再在软件里面安装虚拟机,而ESXi相当于一个linux操 ...

  9. 公式b-(a-b)

  10. Uncaught ReferenceError: Vue is not defined(之一)

    报错信息 Uncaught ReferenceError- Vue is not defined 报错代码示例 <body> <div id="app"> ...