日期:2014-05-16  浏览次数:20648 次

nanosleep小延时不能实现,这个函数和select、sleep用什么区别
在做录像回放快进的时候,需要毫秒级延时;
用的是nanosleep,但是在延时设定小于20ms的时候,就无法实现,感觉从调用nanosleep到返回,差不多用了20ms(排除了线程优先级问题),我想问问nanosleep是什么机制,和select和sleep用什么区别;
请大牛指点!!!

------解决方案--------------------
man
The suspension time may be longer than requested  because  the  argument
       value  is  rounded  up to an integer multiple of the sleep resolution or because of the scheduling of other activity by the
       system. But, except for the case of being interrupted by a signal, the suspension time shall not  be  less  than  the  time
       specified by rqtp, as measured by the system clock CLOCK_REALTIME.
------解决方案--------------------
linux下时间相关的函数的精度都基于linux内核中jiffies,而jiffies取决定系统的可编程计时器的值,也就是HZ, 一般linux x86的HZ值是100,也就是每秒振动十次,因此精度就是10ms.所以系统os时间精度最高只能是10ms,不管是用nanosleep还是select获得的最高精度只能是10ms.

如果想获得更高精度的时间,需要访问时间时钟rtc寄存器,在linux中可能通过ioctl访问/dev/rtc来获得,具体参考想关资料