Is there a way to create an accurate delay() function without hogging the CPU?
I tried this, it's accurate, but it still hogs the CPU.
Code:void delay(int a) { clock_t sec; sec = clock(); while((clock() - sec) < a); }
Is there a way to create an accurate delay() function without hogging the CPU?
I tried this, it's accurate, but it still hogs the CPU.
Code:void delay(int a) { clock_t sec; sec = clock(); while((clock() - sec) < a); }
depending on the OS - yes.
For example in windows WaitForSingleObject with the handle of the curent thread will give the very exact result
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
In your Help >> Index, check out delay() and sleep().
If you really want something else done by the cpu in the meantime, you may need to do something like this:
delay(1/4th of total delay you want)
call function of code you want executed
delay(1/4th of total delay you want)
call another function/same function as before to execute some code, there.
delay(1/4th of total delay you want)
etc.
Or same idea with sleep().
I've never done it with the Windows API, but it might be possible that way, as well.
Have you checked the Windows forum? Might be even in their FAQ.
You can also check out Sleep() which is Windows-specific (not the same as sleep). I believe you need winuser.h to use it.
You can also use the select() function for extra portability.