C++ 中的 Pause()、Sleep() 和 Wait() 有什么区别? [英] What is the difference between Pause(), Sleep() and Wait() in C++?

查看:139
本文介绍了C++ 中的 Pause()、Sleep() 和 Wait() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 CS106B 斯坦福大学的课程,在完成 Boggle 作业时,我注意到 Windows 上的 Sleep() 函数的行为与 Pause() 函数不同.出于测试目的,我简单地设置了电路板并使用提供的 gboggle.h 文件突出显示 Boggle 立方体,然后删除突出显示.以下是相关代码:

I have been working through the CS106B course 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:

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);
    }
}

如果我使用 Pause(),立方体高亮显示,然后恢复正常.如果我使用 Sleep() 或 Wait(),则立方体永远不会突出显示,并且程序中的延迟甚至发生在绘制板之前而不是在 for 循环之间.相关的Wait()函数:

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:

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

取自此处.我在 Windows XP 上使用 Visual Studio 2005.

taken from here. I am using Visual Studio 2005 on Windows XP.

这些函数之间的什么区别导致它们以这种方式运行?

What difference between these functions causes them to act this way?

我知道睡眠和等待需要整数.我已经使用整数测试它们并看到延迟,但它发生在写入平方之前.抱歉,我之前不清楚.

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:在查看我使用的其他一些库后,我发现 Pause 实际上是简单地暂停图形缓冲区的图形库的一部分.

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.

推荐答案

我以前从未见过暂停命令;也许你可以提供一些代码?

I've never seen the Pause command before; perhaps you could provide some code for it?

Windows 应用程序基于消息泵的想法,而绘画的优先级较低.

Windows apps work on the idea of a message pump, and that painting is a low priority.

如果您在消息泵线程中休眠或等待,那么您会阻止它进一步处理消息,例如绘制屏幕.

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.

您可能会查看 的用法多个并运行第二个消息泵.(猜测这是Pause的正文).

You might look at usage of Wait for multiple and running a second message pump. (guessing this is the body of Pause).

这篇关于C++ 中的 Pause()、Sleep() 和 Wait() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆