偶然发现time_nanosleep()并开始怀疑,你什么时候想要使用它?
此外,甚至可以在像PHP这样的脚本语言中使用这么短的时间跨度吗?
解决方法
不,它永远不可能使用单纳秒.只有Linux中的上下文切换通常在几百纳秒内,不包括Zend引擎的开销.
我检查了PHP源代码,它只是一个围绕C nanosleep函数的简单包装器. nanosleep的手册页说:
nanosleep() suspends the execution of the calling thread until
either at least the time specified in *req has elapsed…
Compared to sleep(3) and usleep(3),nanosleep() has the following
advantages: it provides a higher resolution for specifying the
sleep interval
所以这就是你要指定间隔的小小.由于usleep以微秒(数千纳秒)指定,因此对于一些非常小的睡眠间隔,它的基本单位可能太大.