[英]Win32 Application with high CPU usage
It is my first time working with Windows applications and I am a bit frustrated. 这是我第一次使用Windows应用程序,我有些沮丧。 I just have a simple code that creates a Win32(Visual C++) GUI application, but as soon as I start the executable my CPU goes up to 30%(Over the application), in my search to solve this I have found someone saying that sleep for 10ms in the message loop(GetMessage or PeekMessage) would solve this and it indeed did. 我只有一个简单的代码可以创建Win32(Visual C ++)GUI应用程序,但是一旦启动可执行文件,我的CPU就会上升30%(超过应用程序),在寻找解决方案的过程中,我发现有人说在消息循环中睡眠10ms(GetMessage或PeekMessage)将解决此问题,并且确实做到了。 But as I don't really enjoy sleeping my code all of a sudden and by the fact that this is a event handler loop I would love an explanation on how to solve this. 但是由于我不是真的很喜欢突然间休眠我的代码,而且由于这是一个事件处理程序循环,所以我很想解释如何解决这个问题。
A peek on the "troublesome area". 窥视“麻烦区域”。
while (TRUE) {
if (PeekMessage(&g_Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&g_Msg); // translate keystroke messages into the right format
DispatchMessage(&g_Msg); //Send the message to the WindowProc function
if (g_Msg.message == WM_QUIT)
break;
} else {
// Run d3d code here
}
// Sleep(1); REDUCES CPU!
}
Thanks in advance 提前致谢
Caio Guedes Caio Guedes
A standard Windows application loop uses GetMessage. 一个标准的Windows应用程序循环使用GetMessage。 GetMessage blocks until there's input or something in the application that needs to run. GetMessage阻塞,直到应用程序中有输入或需要运行的内容为止。
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
What you have coded is suitable and standard for a game - when you want DirectX to render frames as fast as possible while still pumping messages for other in-process windows and Win32 APIs. 您编写的代码适合于游戏,并且是游戏的标准-当您希望DirectX尽可能快地渲染帧,同时仍在为其他进程内窗口和Win32 API传送消息时。 What you have coded is essentially using an entire core of CPU when you code it this way. 以这种方式进行编码时,您所编码的实际上是在使用整个CPU核心。 (If you have a quad-core CPU, you're using at least 25%, so that 30% measurement seems accurate). (如果您使用的是四核CPU,则至少要使用25%,这样30%的测量似乎是准确的)。
The periodic Sleep(0)
or Sleep(1)
statement is useful to allow your main thread to yield to other threads and processes. 定期的Sleep(0)
或Sleep(1)
语句可用于使您的主线程屈服于其他线程和进程。 You'll sacrifice a little frame rate, but sometimes that's necessary if the game has other threads for networking and sound. 您将牺牲一点帧速率,但是有时如果游戏具有用于网络和声音的其他线程,则这是必需的。 But since you have a multi-core CPU, then you might not need to do that since the other cores can service those threads. 但是,由于您具有多核CPU,因此您可能不需要这样做,因为其他核可以为这些线程提供服务。
In the old days, before hyperthreading and multi-core, doing the Sleep
statement was the normal way to not be a total resource hog. 在过去,在超线程和多核之前,执行Sleep
语句是不浪费资源的正常方法。 It allowed other background applications on Windows to be able to run. 它允许Windows上的其他后台应用程序能够运行。
You should use GetMessage(), it's a blocking function. 您应该使用GetMessage(),这是一个阻止函数。 It will block until there is something to process. 它将阻塞,直到需要处理的内容为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.