Linux中的sleep、usleep、nanosleep、poll和select
在进行Linux C/C++编程时,可调用的sleep函数有好多个,那么究竟应当调用哪一个了?下表列出了这几个函数间的异同点,可作为参考:
性质 |
精准度 |
线程安全 |
信号安全 |
||
sleep |
libc库函数 |
秒 |
是 |
不能和alarm同时使用 |
有些是基于alarm实现的,所以不能和alarm同时使用 |
usleep |
libc库函数 |
微秒 |
- |
- |
POSIX.1-2001已将usleep标注为废弃,POSIX.1-2008已删除usleep,应当使用nanosleep替代usleep |
nanosleep |
系统调用 |
纳秒 |
是 |
不确定 |
即使被信号中断,也可实现实际睡眠时长不小于参数指定时长 |
clock_nanosleep |
系统调用 |
纳秒 |
是 |
不确定 |
区别于nanosleep,可选择为相对或绝对时间,其次是可以选择使用哪个时钟 |
poll |
系统调用 |
毫秒 |
是 |
是 |
在协程库libco中可安全使用,如被信号中断,则实际睡眠时长会小于参数指定的时长 |
ppoll |
系统调用 |
纳秒 |
是 |
是 |
如被信号中断,则实际睡眠时长会小于参数指定的时长 |
select |
系统调用 |
微秒 |
是 |
是 |
即使被信号中断,也可实现实际睡眠时长不小于参数指定时长 |
pselect |
系统调用 |
纳秒 |
是 |
是 |
如被信号中断,则实际睡眠时长会小于参数指定的时长 |
C/C++常用封装:
1) 基于nanosleep的毫秒级封装
#include <time.h> void millisleep(uint32_t milliseconds) { struct timespec ts = { milliseconds / 1000, (milliseconds % 1000) * 1000000 }; while ((-1 == nanosleep(&ts, &ts)) && (EINTR == errno)); } |
2) 基于nanosleep的微秒级封装
#include <time.h> void microsleep(uint32_t microseconds) { struct timespec ts = { microseconds / 1000000, (microseconds % 1000000) * 1000 }; while ((-1 == nanosleep(&ts, &ts)) && (EINTR == errno)); } |
3) 基于poll的秒级封装
// 可libco协程库中安全使用 void pollsleep(int milliseconds) { (void)poll(NULL, 0, milliseconds); } |
4) 基于select的毫秒级封装
void selectsleep(int milliseconds) { struct timeval timeout = { milliseconds / 1000, (milliseconds % 1000) }; struct timeval old_timeout = { timeout.tv_sec, timeout.tv_usec }; while (true) { (void)select(0, NULL, NULL, NULL, &timeout); if (timeout.tv_sec<=0 && timeout.tv_usec<=0) break; } } |
如果开发环境是C++11或更高版本,则可直接使用C++标准库提供的:
5) 毫秒睡眠
#if __cplusplus >= 201103L #include <chrono> #include <system_error> #include <thread> std::this_thread::sleep_for(std::chrono::milliseconds(1000)); #endif // __cplusplus >= 201103L |
6) 微秒睡眠
#if __cplusplus >= 201103L #include <chrono> #include <system_error> #include <thread> std::this_thread::sleep_for(std::chrono::microseconds(1000)); #endif // __cplusplus >= 201103L |
上述介绍的sleep函数均不方便控制它们提前结束,如果需要这种sleep,可基于pthread_cond_timedwait实现,实现可参考CEvent源码:
https://github.com/eyjian/libmooon/blob/master/src/sys/event.cpp |
Linux中的sleep、usleep、nanosleep、poll和select的更多相关文章
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- Linux的sleep()和usleep()的使用和区别
Linux的sleep()和usleep()的使用和区别 函数名: sleep头文件: #include <windows.h> // 在VC中使用带上头文件 #include <u ...
- 聊聊 Linux 中的五种 IO 模型
本文转载自: http://mp.weixin.qq.com/s?__biz=MzAxODI5ODMwOA==&mid=2666538919&idx=1&sn=6013c451 ...
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
- Linux中C/C++头文件一览
1.Linux中一些头文件的作用: #include <assert.h> //ANSI C.提供断言,assert(表达式) #include <glib.h> ...
- Linux中的IO复用接口简介(文件监视?)
I/O复用是Linux中的I/O模型之一.所谓I/O复用,指的是进程预先告诉内核,使得内核一旦发现进程指定的一个或多个I/O条件就绪,就通知进程进行处理,从而不会在单个I/O上导致阻塞. 在Linux ...
- 解析Linux中的VFS文件系统之文件系统的来源与简介(一)
最近挂载了N多的文件系统,大致了不同文件系统的相应特性及挂载方式,却还是对Linux的文件系统没有从源码方面去了解.不求甚解确实不好不好. 于是借鉴一些大牛的博客及自己的理解,总结了博客系列: 一.V ...
- Linux的sleep()和usleep()
1.sleep和usleep都是linux中的程序挂起函数.只是时间的单位不一样. 2. sleep的基本单位是s(秒),也可以用m(分).h(小时). 例: sleep 1 : 挂起1秒 sleep ...
- Linux中Nginx安装与配置详解
转载自:http://www.linuxidc.com/Linux/2016-08/134110.htm Linux中Nginx安装与配置详解(CentOS-6.5:nginx-1.5.0). 1 N ...
随机推荐
- andorid UI事件 监听器
gridlayout.xml <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns ...
- Activity和Intent
- hdu 1059 (多重背包) Dividing
这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...
- C语言中内存分布及程序运行中(BSS段、数据段、代码段、堆栈)
BSS段:(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配. 数据段 : ...
- 探索未知种族之osg类生物---器官初始化二
那我们回到ViewerBase::frame函数中来,继续看看为什么osg生命刚刚出生的时候会大哭,除了初始化了eventQuene和cameraManipulator之外还对那些器官进行了初始化.在 ...
- BZOJ1093或洛谷2272 [ZJOI2007]最大半连通子图
BZOJ原题链接 洛谷原题链接 和 Going from u to v or from v to u?(题解)这道题类似,只不过是求最大子图的大小和个数而已. 一样用\(tarjan\)求强连通分量, ...
- VisualStudio使用技巧
控制台工程去除黑框 刚学习OpenGL,绘制图形的时候,如果不进行设置,运行的时候会先出现黑窗口再出现Windows窗口.其实要去除控制台窗口非常简单,只需要修改工程设置,把子系统改成Windows, ...
- tableView的cell之间间隔显示空白区域
//再要创建的cell中修改frame - (void)setFrame:(CGRect)frame{ frame.origin.x += ; frame.origin.y += ; frame.si ...
- js 文件下载 进度条
js: /** * 下载文件 - 带进度监控 * @param url: 文件请求路径 * @param params: 请求参数 * @param name: 保存的文件名 * @param pro ...
- STL基础1:vector
#include <iostream> #include <vector> #include <algorithm> #include <numeric> ...