Linux实现定时器
来源:互联网 发布:mac怎么卸载adobe 编辑:程序博客网 时间:2024/05/29 13:57
定时器实现的方式有很多。
1.alarm产生一个超时信号,在信号处理函数中再次alarm一下。
2.libevent和nginx的做法是用epoll/poll的超时时间来做,不过这需要额外的二叉堆/红黑树结构来维护。
3.muduo中使用了一种新的方式timerfd_*非常方便,使用的时候像IO事件一样直接扔进reactor中即可。
下面是代码:
#include <sys/timerfd.h> #include <sys/epoll.h>#include <unistd.h>#include <stdint.h>#include <iostream>using namespace std;const int EPOLL_SIZE = 10;int main(int argc, char* argv[]){ int tfd, epfd, nfds; struct epoll_event event; struct epoll_event events[EPOLL_SIZE]; //创建timerfd, CLOCK_REALTIME为绝对时间,TFD_NONBLOCK为非阻塞 tfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK); if (tfd < 0) { cerr << "timerfd_create error!" << endl; return -1; } struct timespec startTime, intervalTime; startTime.tv_sec = 0; startTime.tv_nsec = 1; //相当于立即到达超时时间 intervalTime.tv_sec = 3; //首次超时后,每三秒超时一次 intervalTime.tv_nsec = 0; struct itimerspec newValue; newValue.it_value = startTime; newValue.it_interval = intervalTime; //设置超时时间,且为相对时间 if (timerfd_settime(tfd, 0, &newValue, NULL) < 0) { cerr << "timerfd_settime error!" << endl; return -1; } //用epoll来监听描述符 epfd = epoll_create(EPOLL_SIZE); if (epfd < 0) { cerr << "epoll_create error!" << endl; return -1; } event.data.fd = tfd; event.events = EPOLLIN; if (epoll_ctl(epfd, EPOLL_CTL_ADD, tfd, &event) < 0) { cerr << "epoll_ctl error!" << endl; return -1; } uint64_t count = 0; while (1) { //非阻塞等待 nfds = epoll_wait(epfd, events, EPOLL_SIZE, 0); if (nfds == 0) continue; for (int i = 0; i < nfds; i++) { if (events[i].events & EPOLLIN) { uint64_t data; read(events[i].data.fd, &data, sizeof(uint64_t)); count += data; cout << "read: " << data << ", timer count: " << count << endl; } } } return 0;}
参考:
http://www.jianshu.com/p/66b3c75cae81
阅读全文
1 0
- Linux定时器的实现
- Linux定时器的实现
- Linux定时器实现
- linux定时器的实现
- Linux实现定时器
- LINUX 定时器的实现方式
- Linux下实现定时器功能
- linux定时器的实现方法
- linux定时器的实现方法
- linux下定时器的实现
- linux内核定时器的实现
- Linux表层程序实现定时器
- linux内核定时器的实现
- linux定时器的实现方法
- linux定时器的实现方法
- linux 下定时器的实现
- Linux上定时器的实现
- 模仿linux内核定时器代码,用python语言实现定时器
- Spring-Boot学习笔记(一) 项目搭建和HelloWold
- 数据库索引设计与优化读书笔记--《四》为SELECT语句创建理想的索引
- 简单ngrok内网穿透工具使用
- VS2010 编译jrtplib
- [附中OJ 1491]小K的农场
- Linux实现定时器
- pci 设备使用pci_alloc_consistent 来申请dma可以简化code
- Activity之间的数据问题
- 字符串处理
- iOS开发之UIColor类拓展,可通过上百种颜色名字生成颜色 可通过十六进制生成颜色
- Go语言学习之path/filepath包(the way to go)
- 关于百度地图再fragment内重叠问题
- PAT (Advanced Level) Practise 1089 Insert or Merge (25)
- 应用程序运行时,动画的执行与代码的逻辑顺序不匹配,即指定控件未出现动画效果,而其他控件却出现动画效果