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

I worked through the Stanford CS106B course, and while doing the Boggle job, I noticed that the Sleep () function on Windows behaves differently than the Pause () function. For testing purposes, I just installed the board and used the provided gboggle.h file to select the Boggle cubes and then remove the selection. Below is the corresponding 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 are allocated and then returned to their normal state. If I use Sleep () or Wait (), the cubes are never allocated, and the delay in the program occurs before the board is drawn, and not between for loops. Corresponding Wait () function:

 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.

What is the difference between these functions that makes them act this way?

Edit: I know Sleep and wait require integers. I checked them with integers and saw a delay, but this happens before the squares are written. Sorry, I didn’t know about this before.

Edit2: After looking at some of the other libraries that I used, I found that Pause is actually part of a graphics library that simply pauses the graphics buffer.

+4
source share
3 answers

I have never seen the Pause command; maybe you could give him some kind of code?

Windows applications are working on the idea of ​​messaging, and this picture has a low priority.

If you sleep or wait in a message pump flow, you block it from further processing messages, such as drawing a screen.

You need to send messages to the pump so that it can work.

You can take a look at the use of Wait for multiple instances and start the second message pump. (assuming this is the body of the Pause).

+1
source

A sleep wants an integer in milliseconds, and you give it 0.5, so you expect 0 milliseconds. The wait function also accepts ints, so it has the same problem.

Also, the standby function is blocked. While you wait, your application is busy and uses the CPU to wait. While the Sleep Sleep function pauses the current thread, which means that your application really does nothing (does not use CPU resources) until the time runs out.

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

EDIT: It may be that the HighlightCube results are first visible when the application returns to this event loop, and then these cubes are drawn. So you just select them, then wait, and then don't select them. Then your function returns, and the application finally receives them. This is quite obvious, since Sleep (as well as your expectation) simply blocks the application from processing any events (including window paint events). I believe Pause prevents this by returning to the event loop. In fact, this is what Greg Domizhan wrote.

+5
source

Since wait accepts an int parameter, calling it with 0.5 (as you use for Pause as an example) will cause 0.5 to be truncated to 0, so you won't get any delay.

0
source

All Articles