nanosleep 纳秒级延迟

本文介绍了如何利用nanosleep函数进行纳秒级的延迟操作。nanosleep允许程序员暂停进程,直到指定的时间点恢复。它涉及到了CLOCK_REALTIME、CLOCK_PROCESS_CPUTIME_ID和CLOCK_THREAD_CPUTIME_ID这三种计时时钟类型。在调用过程中,进程可能因信号中断,未达到预期延迟时间,此时函数返回-1,并在rem结构体中保存剩余时间。
摘要由CSDN通过智能技术生成

1. nanosleep 测试

函数的原型如下:
int clock_gettime(clockid_t clk_id, struct timespect *tp);

clockid_t clk_id用于指定计时时钟的类型,对于我们Programmer以下三种比较常用:
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.

int nanosleep(const struct timespec *req, struct timespec *rem);

 struct timespec
 {
                      time_t  tv_sec;         /* seconds */
                      long    tv_nsec;        /* nanoseconds */
   };

这个函数功能是暂停某个进程直到你规定的时间后恢复,参数req就是你要暂停的时间,其中req->tv_sec是以

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值