本文介绍了在Mac OS X 10.9上,什么可以使nanosleep准确地在10秒钟内漂移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的程序的一部分需要睡眠10毫秒.通常,我为此使用boost lib,但是有时它会休眠10010毫秒,因此我尝试替换

A part of my program need to sleep for 10 milliseconds. Normally I use the boost lib for this, but it sometimes sleep for 10010 milliseconds, so I tried replacing

boost::this_thread::sleep_for(boost::chrono::milliseconds(read_delay_ms));

使用

struct timespec a;
a.tv_sec  = 0;
a.tv_nsec = read_delay_ms * 1000000;
int rc = nanosleep( &a, NULL );

毫不奇怪,使用nanosleep有时还会休眠10010毫秒(sleep_for是在Mac上使用nanosleep()实现的.)

Not surprisingly the use of nanosleep also sometimes sleep for 10010 milliseconds (sleep_for is implemented using nanosleep() on mac).

我的程序很复杂,因此我无法创建一个说明该问题的小例子,我正在研究这个问题.以下是一些要点:

My program is complex so I have not been able to create a small example that illustrates the issue, I am working on this. Here are some highlights:

  • 这是一个使用boost :: python作为桥梁用C ++编写的python扩展

  • It is a python extension written in C++ using boost::python as bridge

在使用boost :: asio

Uses boost::threads for asynchrony tasks using boost::asio

此问题仅在mac OS X 10.9上可见.在Mac OS X 10.8及更低版本中看不到它,在linux,win,iOS和android上也看不到.

The issue is only seen on mac OS X 10.9. It is not seen on Mac OS X 10.8 and below, and not seen on linux, win, iOS and android.

为了在我的代码中查找错误,增强lib或sys函数,欢迎您提供任何帮助或建议.

In order to find the error in either my code, boost lib or sys functions, any help or suggestions are more that welcome.

推荐答案

App Nap可能是造成这种情况的原因.它是在10.9版中引入的,并以引起这种意外而闻名.

App Nap is most probably the reason for this. It was introduced in 10.9 and is already known for causing such surprises.

NSProcessInfo具有三种新的临时禁用App Nap的方法:beginActivityWithOptions:reason:endActivity:performActivityWithOptions:reason:block:.

NSProcessInfo has new three methods for temporarily disabling App Nap: beginActivityWithOptions:reason:, endActivity:, performActivityWithOptions:reason:block:.

您还可以通过在应用程序域的NSAppSleepDisabled用户默认值中写入布尔YES来禁用它.

You can also disable it by writing boolean YES into NSAppSleepDisabled user default of your application's domain.

这篇关于在Mac OS X 10.9上,什么可以使nanosleep准确地在10秒钟内漂移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:24