A sleep function that is less than a millisecond is possible
I found that sleep (0) worked for me. In a system with a load of almost 0% on the CPU in the task manager, I wrote a simple console program, and the sleep (0) function slept for 1-3 consecutive microseconds, which is less than a millisecond.
But from the above answers in this thread, I know that the amount of sleep (0) can vary much more wildly than it does on systems with a large processor load.
But, as I understand it, the sleep function should not be used as a timer. It should be used so that the program uses the lowest percentage of the processor as possible and runs as often as possible. For my purposes, such as moving a projectile across the screen in a video game, is much faster than one pixel per millisecond, sleep (0) works, I think.
You just make sure that the waiting interval is less than the longest time that he will sleep. You do not use sleep as a timer, but only so that the game uses the minimum percentage of the processor. You would use a separate function that has nothing to do, it's a dream to find out when a certain amount of time has passed, and then move one pixel across the screen on the screen - at a time of 1/10 s of a millisecond or 100 microseconds.
The pseudocode will look something like this.
while (timer1 < 100 microseconds) { sleep(0); } if (timer2 >=100 microseconds) { move projectile one pixel } //Rest of code in iteration here
I know that the answer may not work for advanced problems or programs, but it may work for some or many programs.
rauprog Oct 31 '15 at 5:33 2015-10-31 05:33
source share