//函数原型 int nanosleep(struct timespec *req, struct timespec *rem)
//参数列表:
// req:要求的睡眠时间
// rem:剩余的睡眠时间
//返回值:
// 0:成功;-1,失败,errno保存错误代码
//目的:纳秒级别的延迟
int nano_delay(long delay)
{
struct timespec req, rem;
long nano_delay = delay;
int ret = 0;
while(nano_delay > 0)
{
rem.tv_sec = 0;
rem.tv_nsec = 0;
req.tv_sec = 0;
req.tv_nsec = nano_delay;
if(ret = (nanosleep(&req, &rem) == -1))
{
printf("nanosleep failed.\n");
}
nano_delay = rem.tv_nsec;
};
return ret;
}
//测试,纳秒级延迟的误差率
int main()
{
int ret = 0;
long delay = 0;
struct timespec start, end, interv;
for(delay = 1000; delay < 1000*1000; delay+=1000)
{
curr_time(&start);
nan
nanosleep纳秒级延迟
最新推荐文章于 2024-07-08 13:50:02 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)