在进行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的更多相关文章

  1. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  2. Linux的sleep()和usleep()的使用和区别

    Linux的sleep()和usleep()的使用和区别 函数名: sleep头文件: #include <windows.h> // 在VC中使用带上头文件 #include <u ...

  3. 聊聊 Linux 中的五种 IO 模型

    本文转载自: http://mp.weixin.qq.com/s?__biz=MzAxODI5ODMwOA==&mid=2666538919&idx=1&sn=6013c451 ...

  4. 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 ...

  5. Linux中C/C++头文件一览

    1.Linux中一些头文件的作用: #include <assert.h>       //ANSI C.提供断言,assert(表达式) #include <glib.h>  ...

  6. Linux中的IO复用接口简介(文件监视?)

    I/O复用是Linux中的I/O模型之一.所谓I/O复用,指的是进程预先告诉内核,使得内核一旦发现进程指定的一个或多个I/O条件就绪,就通知进程进行处理,从而不会在单个I/O上导致阻塞. 在Linux ...

  7. 解析Linux中的VFS文件系统之文件系统的来源与简介(一)

    最近挂载了N多的文件系统,大致了不同文件系统的相应特性及挂载方式,却还是对Linux的文件系统没有从源码方面去了解.不求甚解确实不好不好. 于是借鉴一些大牛的博客及自己的理解,总结了博客系列: 一.V ...

  8. Linux的sleep()和usleep()

    1.sleep和usleep都是linux中的程序挂起函数.只是时间的单位不一样. 2. sleep的基本单位是s(秒),也可以用m(分).h(小时). 例: sleep 1 : 挂起1秒 sleep ...

  9. Linux中Nginx安装与配置详解

    转载自:http://www.linuxidc.com/Linux/2016-08/134110.htm Linux中Nginx安装与配置详解(CentOS-6.5:nginx-1.5.0). 1 N ...

随机推荐

  1. redis集群的ruby环境

    redis-4.0.3.gem 下载 https://rubygems.org/gems/redis/ 按照视频在这个地方出错: [root@lx opt]# gem install redis- E ...

  2. node.js中对Event Loop事件循环的理解

    javascript是单线程的,所以任务的执行都需要排队,任务分为两种,一种是同步任务,一种是异步任务. 同步任务是进入主线程上排队执行的任务,上一个任务执行完了,下一个任务才会执行. 异步任务是不进 ...

  3. Vue 局部组件和全局组件的使用

    <template> <div id="app"> <!--<img alt="Vue logo" src="./ ...

  4. Fiddler抓包使用教程-安装配置

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72876628 本文出自[赵彦军的博客] Fiddler是什么? Fiddler是一 ...

  5. spring boot (二):使用fastJson解析json数据

    如果我们想在spring boot中使用第三方的json解析框架: 1)我们需要在pom.xml文件中引入第三方包的依赖; 2)实现方法: 方法1 需要在启动类中继承WebMvcConfigurerA ...

  6. LED

    LED 时间限制: 1 Sec  内存限制: 128 MB 题目描述 数字显示器题目描述:最近学校晚上文化广场的人很多哇,原因是晚上大屏幕会放电影.无聊的艾神和x73也决定一起去文化大广场看一场电影, ...

  7. Vim 基本配置

    1.关闭vi的一致性模式 set nocompatible 2.配置backspace的工作方式 set backspace=indent,eol,start 3.显示行号 set number 4. ...

  8. Python之paramiko模块

    今天我们来了解一下python的paramiko模块 paramiko是python基于SSH用于远程服务器并执行相应的操作. 我们先在windows下安装paramiko 1.cmd下用pip安装p ...

  9. nginx指令中的优化(配置文件)

    nginx指令中的优化(配置文件)worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数.worker_cpu_affinity 00000001 0000 ...

  10. oracle 视图带参数

    -- create or replace package p_view_param is --参数一 function set_ID(num number) return number; functi ...