c++ - 当主窗口被隐藏时,pthread wait() 花费的时间比预期的要长

标签 c++ multithreading macos pthreads

我们在 MacOS 上的线程中等待时遇到问题并且主窗口被隐藏,即使我们请求它等待 100 毫秒,等待函数也最多需要 10 秒。

主程序在 Cocoa 窗口上运行,另一个线程永久运行,每次迭代等待 100 毫秒。 当主窗口可见时一切正常,但是一旦窗口被隐藏,问题就会在一段时间后开始发生,即等待开始等待几秒钟。我们怀疑系统不再经常唤醒应用程序,因为它不再可见。

我们正在使用 pthread_con_wait,但使用 usleep 或 boost::sleep 时会发生同样的问题(它们可能在底层使用相同的东西)。

有没有办法阻止这种情况或设置一个标志来告诉系统我们仍在运行并且我们想要被唤醒?

谢谢

最佳答案

如果是 OS v10.9 或更高版本,您的应用程序可能正在休眠: Power Efficiency Guide for Mac Apps

文档说,可以用 NSProcessInfo 来阻止它类。

Managing Activities

The system has heuristics to improve battery life, performance, and responsiveness of applications for the benefit of the user. You can use the following methods to manage activities that give hints to the system that your application has special requirements:

beginActivityWithOptions:reason:

endActivity:

performActivityWithOptions:reason:usingBlock:

In response to creating an activity, the system will disable some or all of the heuristics so your application can finish quickly while still providing responsive behavior if the user needs it.

...

id activity = [[NSProcessInfo processInfo] ?  
beginActivityWithOptions:NSActivityLatencyCritical  
                  reason:@"Good Reason"];  
// Perform some work.  
[[NSProcessInfo processInfo] endActivity:activity];

注意,

NSActivityLatencyCritical

Flag to indicate the activity requires the highest amount of timer and I/O precision available.

IMPORTANT Very few applications should need to use this constant.

关于c++ - 当主窗口被隐藏时,pthread wait() 花费的时间比预期的要长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26416876/

相关文章:

c++ - 如何更改工具栏中 QAction 的颜色?

c++ - std::initializer_list 作为构造函数的模板参数

macos - 用window.open打开一个在所有工作区中可见的 Electron float 窗口

python - ImportError : No module named pandas. Pandas 安装 pip

c++ - src/libdyld/MachOObject.cpp 中的编译错误;编译亲爱的

没有指针的 C++ 段错误 (HackerRank)

Php:何时使用 pthread

java - 使用线程 java 阻塞输出直到输入完成

python - 在嵌入式 PyQt 应用程序中使用 QThread

C:多线程