windows C++ 中的 Pause()、Sleep() 和 Wait() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6005255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Pause(), Sleep() and Wait() in C++?
提问by acmshar
I have been working through the CS106Bcourse from Stanford, and while completing the Boggle assignment, I have noticed that the Sleep() function on Windows behaves differently from the Pause() function. For testing purposes, I have simply set up the board and used the provided gboggle.h file to highlight the Boggle cubes, then remove the highlighting. The following is the relevant code:
我一直在学习斯坦福大学的 CS106B课程,在完成 Boggle 作业时,我注意到 Windows 上的 Sleep() 函数的行为与 Pause() 函数不同。出于测试目的,我简单地设置了电路板并使用提供的 gboggle.h 文件突出显示 Boggle 立方体,然后删除突出显示。以下是相关代码:
for(int row = 0; row < board.numRows(); row++)
{
for(int col = 0; col < board.numCols(); col++)
{
HighlightCube(row, col, true);
}
}
Pause(0.5);
for(int row = 0; row < board.numRows(); row++)
{
for(int col = 0; col < board.numCols(); col++)
{
HighlightCube(row, col, false);
}
}
If I use Pause(), the cubes highlight, then return to normal. If I use Sleep() or Wait(), the cubes never highlight, and the delay in the program occurs before the board is even drawn rather than between the for loops. The relevant Wait() function:
如果我使用 Pause(),多维数据集会高亮显示,然后恢复正常。如果我使用 Sleep() 或 Wait(),立方体永远不会突出显示,并且程序中的延迟甚至发生在绘制板之前而不是在 for 循环之间。相关的 Wait() 函数:
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
taken from here. I am using Visual Studio 2005 on Windows XP.
取自这里。我在 Windows XP 上使用 Visual Studio 2005。
What difference between these functions causes them to act this way?
这些功能之间的什么区别导致它们以这种方式行事?
Edit: I am aware that Sleep and wait require integers. I have tested them using integers and see a delay, but it occurs before the squares are written. Sorry I was not clear about that previously.
编辑:我知道睡眠和等待需要整数。我已经使用整数测试它们并看到延迟,但它发生在写入平方之前。对不起,我之前不清楚。
Edit2: After looking through some of the other libraries I used, I found that Pause is, in fact, part of the graphics library that simply pauses the graphics buffer.
Edit2:查看我使用的其他一些库后,我发现 Pause 实际上是图形库的一部分,它只是暂停图形缓冲区。
采纳答案by Greg Domjan
I've never seen the Pause command before; perhaps you could provide some code for it?
我以前从未见过暂停命令;也许你可以为它提供一些代码?
Windows apps work on the idea of a message pump, and that painting is a low priority.
Windows 应用程序基于消息泵的想法,而绘画的优先级较低。
If you sleep or wait in the message pump thread then you block it from doing any further handling of messages such as drawing the screen.
如果您在消息泵线程中休眠或等待,那么您将阻止它进一步处理消息,例如绘制屏幕。
You need to yield to the message pump so it can do it's work.
您需要屈服于消息泵,以便它可以完成它的工作。
You might look at usage of Wait for multipleand running a second message pump. (guessing this is the body of Pause).
您可能会查看Wait for multiple 的用法并运行第二个消息泵。(猜测这是Pause的主体)。
回答by Christian Rau
Sleep wants an integer as milliseconds and you give it 0.5, so your wait for 0 milliseconds. Your wait function also takes ints, so it has the same problem.
Sleep 需要一个整数作为毫秒,你给它 0.5,所以你等待 0 毫秒。你的等待函数也需要整数,所以它有同样的问题。
Also your wait function is blocking. As long as you are waiting, your application is busy and uses the CPU for, well waiting. Whereas the windows Sleep function suspends the current thread, meaning your application really does nothing (does not use any CPU resources), until the time is over.
您的等待功能也被阻塞。只要您在等待,您的应用程序就会很忙,并且会使用 CPU 等待。而 windows Sleep 函数会挂起当前线程,这意味着您的应用程序实际上什么都不做(不使用任何 CPU 资源),直到时间结束。
EDIT:I don't know what Pause does, as it is not a WinAPI function.
编辑:我不知道 Pause 是做什么的,因为它不是 WinAPI 函数。
EDIT:It could be, that the results of HighlightCube are first seen, when the application get's back to it's event loop and then these cubes are drawn. This way you just highlight them, then wait, then un-highlight them. Then your function returns and the application gets to finally draw them. That is quite obvious, as Sleep (and also your wait) just block the application from processing any events (including window paint events). I suppose the Pause prevents that by checking back to the event loop. Actually that's what Greg Domjan already wrote.
编辑:当应用程序返回到它的事件循环然后绘制这些立方体时,可能首先看到 HighlightCube 的结果。通过这种方式,您只需突出显示它们,然后等待,然后取消突出显示它们。然后你的函数返回,应用程序最终绘制它们。这很明显,因为睡眠(以及您的等待)只会阻止应用程序处理任何事件(包括窗口绘制事件)。我想暂停通过检查回事件循环来防止这种情况。实际上,这就是 Greg Domjan 已经写的内容。
回答by Will A
Since wait
takes an int parameter, calling it with 0.5 (as you're example uses for Pause
) will result in the 0.5 being truncated to 0, so you'll get no delay.
由于wait
采用 int 参数,因此使用 0.5 调用它(如您的示例所使用的 for Pause
)将导致 0.5 被截断为 0,因此您不会有任何延迟。