I've a -windows only/winapi- c++ application that needs to execute some instruction at specific small intervals, the code that's to be periodically run is small, and takes under a millisecond, using sleep or std::unique_lock<std::mutex> lock{ mutex }; if (!cv.wait_for(lock, std::chrono::milliseconds{ timeInMillies }, [] {return condition_to_be_met; }))
for timing the execution results in a variance of a few milliseconds,is there a more efficient/ more timing guaranteed way of doing achieving that, even if it uses a windows only API?
Asked
Active
Viewed 37 times
1

rimuru
- 29
- 3
-
Are you willing to "burn" a few CPU cycles while waiting for the exact moment? Because then you could simply busy-wait for a few pico/nano/micro seconds... – André Aug 16 '22 at 07:13
-
@André yes, but is there nothing more efficient than that? & what's the most efficient way to do that anyway since you put burn into quotations – rimuru Aug 16 '22 at 07:50
-
*"the code that's to be periodically run is small, and takes under a millisecond"* - That's an alarming statement. A hypothetical CPU that executes one instruction per clock cycle, clocked at 1GHz, runs 1 million instructions in a millisecond. What exactly does *"small"* mean to you? – IInspectable Aug 16 '22 at 09:03
-
@IInspectable I meant that the total time of the code in incomparable to the variance of the timer, it's just some mathematical stuff, on the time stamps & some other data – rimuru Aug 16 '22 at 10:28