开发者

What is the difference between Pause(), Sleep() and Wait() in C++?

开发者 https://www.devze.com 2023-03-05 18:42 出处:网络
I have been working through theCS106B course from Stanford, and while completing the Boggle assignment, I have noticed that the Sleep() function on Windows behaves differently from the Pause() functio

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

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 () + seco开发者_StackOverflow社区nds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

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

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.


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.

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.

EDIT: I don't know what Pause does, as it is not a WinAPI function.

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.


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.

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 multiple and running a second message pump. (guessing this is the body of Pause).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消