```
#ifndef _LOOP_EPOLL_H_
#define _LOOP_EPOLL_H_
#include <sys/epoll.h>
#include <iostream>
#include <unistd.h>
#include <map>
#include <functional>
#include <memory>
#include <thread>
class LoopEpoll {
public:
typedef std::shared_ptr<LoopEpoll> ptr;
typedef std::function<void()> Func;
LoopEpoll() : stop_(false)
{
epfd_ = epoll_create(10);
if (epfd_ < 0) {
std::cout << "Failed to create epoll" << std::endl;
exit(1);
}
}
~LoopEpoll() {
close(epfd_);
}
void stop() { stop_ = true; }
void updateEvent(int fd, int events, Func func) {
auto it = events_.find(fd);
if (it == events_.end()) {
epoll_event e;
e.data.fd = fd;
e.events = events;
events_[fd] = e;
callbacks_[fd] = func;
if (epoll_ctl(epfd_, EPOLL_CTL_ADD, fd, &e) < 0) {
std::cout << "failed to add handler to epoll" << std::endl;
}
} else {
epoll_event &e = events_[fd];
e.events = events;
callbacks_[fd] = func;
if (epoll_ctl(epfd_, EPOLL_CTL_MOD, fd, &e) < 0) {
std::cout << "failed to modify handler to epoll" << std::endl;
}
}
}
void removeEvent(int fd) {
auto it = events_.find(fd);
if (it != events_.end()) {
events_.erase(fd);
callbacks_.erase(fd);
if (epoll_ctl(epfd_, EPOLL_CTL_DEL, fd, NULL) < 0) {
std::cout << "failed to delete handler from epoll" << std::endl;
}
}
}
void run() {
std::thread t(std::bind(loopEpoll, this, 1000));
t.detach();
}
private:
void loopEpoll(int timeout) {
const uint64_t MAX_EVENTS = 1024;
epoll_event events[MAX_EVENTS];
while (!stop_) {
int nfds = epoll_wait(epfd_, events, MAX_EVENTS, timeout);
for (int i = 0; i < nfds; ++i) {
int active_fd = events[i].data.fd;
removeEvent(active_fd);
std::cout << "epoll_wait active_fd=" << active_fd << std::endl;
Func cb = callbacks_[active_fd];
cb();
}
}
}
private:
int epfd_;
bool stop_;
std::map<int, epoll_event> events_;
std::map<int, Func> callbacks_;
};
#endif
```
```
#ifndef _TIMER_H_
#define _TIMER_H_
#include <sys/timerfd.h>
#include <iostream>
#include <unistd.h>
#include <map>
#include <functional>
#include <atomic>
#include "loop_epoll.h"
std::atomic<int> g_sequence_creator;
class Timer {
public:
typedef std::function<void()> Func;
Timer(LoopEpoll::ptr loopEpoll) : loopEpoll_(loopEpoll)
{
int timer_fd_ = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (timer_fd_ < 0) {
std::cout << "timerfd_create faild!" << std::endl;
}
}
~Timer() {
::close(timer_fd_);
}
int runOnce(uint64_t when, Func cb) {
}
int runEvery(uint64_t when, Func cb, int interval) {
}
private:
std::multimap<uint64_t, Func> callbacks_;
int timer_fd_;
LoopEpoll::ptr loopEpoll_;
};
#endif
```
- 空白目录
- 算法
- 排序
- 冒泡排序
- 选择排序
- 插入排序
- 归并排序
- 快速排序
- 计数排序
- 桶排序
- 基数排序
- 希尔排序
- 堆排序
- 二分查找
- 最小堆
- 最小索引堆
- 平衡二叉树(AVL tree)
- bitmap位图
- 布隆过滤器
- hashmap
- topK
- 跳表
- LRU Cache
- kmp
- 最小堆和堆排序
- 最短路径
- C++
- 运行时类型判断RTTI
- C++反射
- 手动实现智能指针
- 序列化实现
- rpc实现
- std::forward
- 函数指针的妙用
- C/C++
- std::function
- 同步队列
- 线程池实现
- std::promise
- 深入理解虚函数
- extern "C" 关键字讲解
- 大端小端的区别
- 简历
- 简历1
- redis
- 数据结构和对象
- sds
- list
- zskiplist
- 腾讯云redis面试题总结
- redis集群部署
- LeetCode
- 目标
- go基础
- 算法快速入门
- 数据结构篇
- 二叉树
- 链表
- 栈和队列
- 二进制
- 基础算法篇
- 二分搜索
- 排序算法
- 动态规划
- 算法思维
- 递归思维
- 滑动窗口思想
- 二叉搜索树
- 回溯法
- 其他
- 剑指offer
- 笔记
- git代理加速
- Linux
- vim大法
- vscode远程不能跳转
- cmake
- 设计模式
- 单例模式
- 简单工厂模式
- 外观模式
- 适配器模式
- 工厂方法模式
- 抽象工厂模式
- 生成器模式
- 原型模式
- 中介者模式
- 观察者模式
- 访问者模式
- 命令模式
- 网络编程
- epoll reactor模式
- linux timerfd系列函数总结
- IO
- mapreduce
- 反射器
- leo通信库
- Mutex
- Condition
- thread
- raft
- 协程
- hook
- 定时器
- 别人的面试经验
- 面试题
- vector崩溃问题
- JAVA
- Linux java环境配置
- ucore
- lab1
- FreeNOS
- leveldb
- 刷题笔记
- 回文串
- 前缀树
- 字符串查找
- 查找两个字符串a,b中的最长公共子串
- 动态规划
- golang
- 顺序循环打印实现
- 数据结构
- rpc运用
- python
- 单例
- 深拷贝浅拷贝
- 链表
- python基础题
- mysql
- 事务
- Linux
- 共享内存
- 刷题记录
- 贪心算法
- 动态规划
- 面试
- 腾讯C++面试
- 微众面试JD
- 迅雷网络面试
- 学习网址
- rabbitMq
