winapi - Thread-safe GetTickCount64 implementation for Windows XP -


i'm targeting windows xp, , need function similar gettickcount64, not overflow.

i couldn't find decent solution correct , thread safe, tried roll own.

here's came with:

ulonglong mygettickcount64(void) {     static volatile dword dwhigh = 0;     static volatile dword dwlastlow = 0;     dword dwtickcount;      dwtickcount = gettickcount();     if(dwtickcount < (dword)interlockedexchange(&dwlastlow, dwtickcount))     {         interlockedincrement(&dwhigh);     }      return (ulonglong)dwtickcount | (ulonglong)dwhigh << 32; } 

is thread safe?

thread safety difficult check correctness, i'm not sure whether it's correct in cases.

on windows timer overflow problem in solved (in games) using queryperformancecounter() functions instead of gettickcount():

double getcycles() const {     large_integer t1;     queryperformancecounter( &t1 );     return static_cast<double>( t1.quadpart ); } 

then can multiply number reciprocal number of cycles per second convert cycles seconds:

void initialize() {     large_integer freq;     queryperformancefrequency( &freq );     double cyclespersecond = static_cast<double>( freq.quadpart );     recipcyclespersecond = 1.0 / cyclespersecond; } 

after initialization, code thread safe:

double getseconds() const {     return getcycles() * recipcyclespersecond; } 

you can checkout full source code (portable between windows , many other platforms) our open-source linderdaum engine: http://www.linderdaum.com


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -