博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
select实现精确定时器
阅读量:7171 次
发布时间:2019-06-29

本文共 929 字,大约阅读时间需要 3 分钟。

hot3.png

select实现精确定时器

int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);struct timeval { long tv_sec; long tv_usec; } /* seconds and microseconds */

秒级定时器

void sec_sleep(unsigned sec) {    int err;    struct timeval tv;    tv.tv_sec = sec;    tv.tv_usec = 0;    do {           err = select(0, NULL, NULL, NULL, &tv);    } while (err < 0 && errno == EINTR);}

毫秒级定时器

void msec_sleep(unsigned long msec) {    int err;    struct timeval tv;    tv.tv_sec = msec / 1000;    tv.tv_usec = (msec % 1000) * 1000;    do {           err = select(0, NULL, NULL, NULL, &tv);    } while (err < 0 && errno == EINTR);}

微秒级定时器

void usec_sleep(unsigned long usec) {    int err;    struct timeval tv;    tv.tv_sec = usec / 1000000;    tv.tv_usec = usec % 1000000;    do {        err = select(0, NULL, NULL, NULL, &tv);    } while (err < 0 && errno == ENTR);}

转载于:https://my.oschina.net/keeplearn/blog/345664

你可能感兴趣的文章
DelayQueue在容错时的使用
查看>>
屏蔽silverlight 4应用中的右键菜单
查看>>
NAT原理简介
查看>>
内联表达式
查看>>
手动添加数据源时DataGridViewComboBoxCell值出问题解决方法
查看>>
函数声明优先于变量
查看>>
HDU-1003 Max Sum 动态规划
查看>>
Silverlight.XNA(C#)跨平台3D游戏研发手记:(九)3D 骨骼动画
查看>>
在.NET外散步之我爱贪吃蛇Python -常见语句(神奇的else)
查看>>
Known Issues
查看>>
文件相关操作工具类——FileUtils.java
查看>>
原:视频直播技术中的参考技术网页
查看>>
linq教程
查看>>
requests从api中获取数据并存放到mysql中
查看>>
23种设计模式之组合模式(Composite)
查看>>
button按钮点击不刷新(前端交流学习:452892873)
查看>>
安卓 使用Gradle生成正式签名apk文件
查看>>
@Html.Raw()
查看>>
ES6 Proxy
查看>>
图的基本算法(BFS和DFS)
查看>>