SleepEx() 期间 CPU 使用率不会下降
我的程序是幻灯片。它在具有其他进程的计算机上运行,因此在等待显示下一张幻灯片时,我调用 SleepEx(N, false),期望它将使用的 CPU 量减少到接近于零(N 在 100 毫秒到 5000 毫秒之间)。在我的开发 XP Pro 机器上,这正是发生的情况,但在我客户的 XP Home 机器上,它在 SleepEx() 期间注册了 30-80% 的 CPU。该代码是单线程,因此使用所有 cpu 的任何内容都在对 SleepEX() 的调用中。有人见过这个吗?
My program is a slideshow. It runs on a machine with other processes, so while it's waiting to display the next slide I call SleepEx(N, false), expecting it to reduce to near-zero the amount of CPU it uses (N is between 100ms and 5000ms). On my development XP Pro machine that's exactly what happens but on my customer's XP Home machine it registers 30-80% CPU during the SleepEx(). The code is a single thread so whatever is using all that cpu is within the call to SleepEX(). Has anyone seen this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哪个进程占用了所有 CPU?如果您使用调试器闯入该进程 - 它在堆栈跟踪中的哪个位置花费了时间?
尝试使用 ProcDump 在达到 CPU 峰值时创建进程转储。然后分析堆栈跟踪以查看它卡在哪里。这样做几次,您就可以很好地了解它花费时间的地方。
Which process is taking all that CPU? If you break into the process with a debugger - where in the stack trace is it spending time?
Try to use ProcDump to create a dump of the process when it reaches that CPU spike. Then analyze the stack trace to see where it's stuck. Do this several times you get a good sampling of where it's spending time.
我以前见过这个。您阻塞主窗口消息处理线程。
如果单线程应用程序具有主窗口消息处理功能,则不应将 Sleep() 函数放置在该应用程序中。窗口应用程序始终应该处理窗口消息而没有明显的延迟,在另一种情况下,它至少会导致应用程序死锁。
后果取决于 Windows 平台、编译器设置和 CPU 配置,通常调试模式下的应用程序有临时解决方法。但如果你启动这样一个使用发布设置编译的应用程序,它可能会消耗一个CPU核心的功能,这会阻塞他的主窗口消息处理线程。
MSDN Sleep() 函数描述中的备注部分清楚地说明了这种情况。
您只需要启动新线程,就可以使用 Sleep() 函数来允许主线程中窗口消息的自由流动。
I have seen this before. You block main window message processing thread.
You should not place Sleep() function in single-threaded application if it has main window message processing function. Windowed application always should process window messages without noticeable delay, in another case it will cause deadlock at least for application.
Consequences depends on windows platform, compiler settings and CPU configuration, usually application in debug mode has temporary workaround. But if you start such application compiled with release settings it can consume one CPU core with function, which have blocked his main window message processing thread.
Remarks section at MSDN Sleep() function description clearly states this situation.
You just have to lauch new thread, to use Sleep() function right there to allow free flow of window messages in main thread.