c++-cliprecisiontime-precisiontimestamping

How can I (reasonably) precisely perform an action every N milliseconds?


I have a machine which uses an NTP client to sync up to internet time so it's system clock should be fairly accurate.

I've got an application which I'm developing which logs data in real time, processes it and then passes it on. What I'd like to do now is output that data every N milliseconds aligned with the system clock. So for example if I wanted to do 20ms intervals, my oututs ought to be something like this:

13:15:05:000
13:15:05:020
13:15:05:040
13:15:05:060

I've seen suggestions for using the stopwatch class, but that only measures time spans as opposed to looking for specific time stamps. The code to do this is running in it's own thread, so should be a problem if I need to do some relatively blocking calls.

Any suggestions on how to achieve this to a reasonable (close to or better than 1ms precision would be nice) would be very gratefully received.


Solution

  • Don't know how well it plays with C++/CLR but you probably want to look at multimedia timers,
    Windows isn't really real-time but this is as close as it gets