title: Accurate C/C++ clock on a multi-core processor with auto-overclock?
body: <p>I have looked into several topics to try to get some ideas on how to make a reliable clock with C or C++. However, I also saw some functions used the processor's ticks and ticks per second to calculate the end result, which I think could be a problem on a CPU with auto-overclock like the one I have. I also saw one of them reset after a while, thus is not really reliable.</p>
  
  <p>The idea is to make a (preferably cross-platform) clock like an in-game one, with a precision better than a second in order to be able to add the elapsed time in the "current session" with the saved time at the end of the program. This would be to count the time spent on a console game that does not have an in-game clock, and on the long run to perhaps integrate it to actual PC games.</p>
  
  <p>It should be able to run without taking too much or all of the CPU's time (or a single core's time for multi-core CPUs) as it would be quite bad to use all these resources just for the clock, and also on systems with auto-overclock (which could otherwise cause inaccurate results).</p>
  
  <p>The program I would like to implement this feature into currently looks like this, but I might re-code it in C (since I have to get back to learning how to code in C++):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  int main()
  {
      cout &lt;&lt; "In game" &lt;&lt; endl;
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <p>On a side-note, I still need to get rid of the PAUSE feature which is Windows-specific, but I think that can be taken care of with a simple "while (char != '\n')" loop.</p>
  
  <p>What I have already skimmed through:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/12743063/using-clock-to-measure-execution-time">Using clock() to measure execution time</a></li>
  <li><a href="https://stackoverflow.com/questions/1468596/calculating-elapsed-time-in-a-c-program-in-milliseconds">Calculating elapsed time in a C program in milliseconds</a></li>
  <li><a href="https://stackoverflow.com/questions/1444428/time-stamp-in-the-c-programming-language/1445808#1445808">Time stamp in the C programming language</a></li>
  <li><a href="https://stackoverflow.com/questions/5248915/execution-time-of-c-program">Execution time of C program</a></li>
  <li><a href="https://stackoverflow.com/questions/2962785/c-using-clock-to-measure-time-in-multi-threaded-programs">C: using clock() to measure time in multi-threaded programs</a></li>
  <li><a href="https://stackoverflow.com/questions/88/is-gettimeofday-guaranteed-to-be-of-microsecond-resolution">Is gettimeofday() guaranteed to be of microsecond resolution?</a></li>
  <li><a href="https://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi-c/37920181#37920181">How to measure time in milliseconds using ANSI C?</a></li>
  <li><a href="https://stackoverflow.com/questions/1487695/c-cross-platform-high-resolution-timer">C++ Cross-Platform High-Resolution Timer</a></li>
  <li><a href="https://stackoverflow.com/questions/275004/timer-function-to-provide-time-in-nano-seconds-using-c">Timer function to provide time in nano seconds using C++</a></li>
  <li><a href="https://stackoverflow.com/questions/17502263/how-to-measure-cpu-time-and-wall-clock-time">How to measure cpu time and wall clock time?</a></li>
  <li><a href="https://stackoverflow.com/questions/17432502/how-can-i-measure-cpu-time-and-wall-clock-time-on-both-linux-windows">How can I measure CPU time and wall clock time on both Linux/Windows?</a></li>
  <li><a href="https://stackoverflow.com/questions/23186287/how-to-measure-time">how to measure time?</a></li>
  <li><a href="https://stackoverflow.com/questions/16299029/resolution-of-stdchronohigh-resolution-clock-doesnt-correspond-to-measureme">resolution of std::chrono::high_resolution_clock doesn&#39;t correspond to measurements</a></li>
  <li><a href="https://stackoverflow.com/questions/46624865/c-how-to-make-timer-accurate-in-linux">C++ How to make timer accurate in Linux</a></li>
  <li><a href="http://gameprogrammingpatterns.com/game-loop.html" rel="nofollow noreferrer">http://gameprogrammingpatterns.com/game-loop.html</a></li>
  <li><a href="https://stackoverflow.com/questions/7286787/clock-accuracy">clock() accuracy</a></li>
  <li><a href="https://stackoverflow.com/questions/46567615/stdchrono-doesnt-seem-to-be-giving-accurate-clock-resolution-frequency">std::chrono doesn&#39;t seem to be giving accurate clock resolution/frequency</a></li>
  <li><a href="https://stackoverflow.com/questions/35592502/clock-function-in-c-with-threads">clock function in C++ with threads</a></li>
  </ul>
  
  <p>(Edit: Extra research, in particular for a C implementation:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/29261814/cross-platform-c-high-precision-event-timer-implementation">Cross platform C++ High Precision Event Timer implementation</a> (no real answer)</li>
  <li><a href="https://stackoverflow.com/questions/13610471/calculating-function-time-in-nanoseconds-in-c-code">Calculating Function time in nanoseconds in C code</a> (Windows)</li>
  <li><a href="https://stackoverflow.com/questions/16275444/how-to-print-time-difference-in-accuracy-of-milliseconds-and-nanoseconds">How to print time difference in accuracy of milliseconds and nanoseconds?</a> (could be the best answer for a C implementation)</li>
  <li><a href="https://stackoverflow.com/questions/14391327/how-to-get-duration-as-int-millis-and-float-seconds-from-chrono">How to get duration, as int milli&#39;s and float seconds from &lt;chrono&gt;?</a> (C++ again) )</li>
  </ul>
  
  <p>The problem is that it is not clear whether some of the mentioned methods, like Boost or SDL2, behave properly with auto-overclock in particular.</p>
  
  <p><strong>TL;DR</strong> : What cross-platform function should I use to make an accurate, sub-second precise counter in C/C++ that could work on multi-core and/or auto-overclocking processors please?</p>
  
  <p>Thanks in advance.</p>