You could build a custom chrono
clock on top of Windows' GetTickCount()
. Then use that clock. In porting, all you would have to do is port the clock. For example, I am not on Windows, but here is what such a port might look like:
#include <chrono>
// simulation of Windows GetTickCount()
unsigned long long
GetTickCount()
{
using namespace std::chrono;
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}
// Clock built upon Windows GetTickCount()
struct TickCountClock
{
typedef unsigned long long rep;
typedef std::milli period;
typedef std::chrono::duration<rep, period> duration;
typedef std::chrono::time_point<TickCountClock> time_point;
static const bool is_steady = true;
static time_point now() noexcept
{
return time_point(duration(GetTickCount()));
}
};
// Test TickCountClock
#include <thread>
#include <iostream>
int
main()
{
auto t0 = TickCountClock::now();
std::this_thread::sleep_until(t0 + std::chrono::seconds(1));
auto t1 = TickCountClock::now();
std::cout << (t1-t0).count() << "ms\n";
}
On my system, steady_clock
happens to return nanoseconds since boot. You may find other non-portable ways of emulating GetTickCount()
on other platforms. But once that detail is done, your clock is solid, and the clock's clients don't need to be any wiser about it.
For me this test reliably outputs:
1000ms
chrono
that might solve the same problem you're facing, though. We just need to know what problem you are facing. – Beechamclock_getttime()
is a POSIX standard interface, which isn't portable to Windows any more thanGetTickCount()
is portable to Linux, so the proposed duplicate is more 'related' than a duplicate. – Matsumotosteady_clock
is the nearest equivalent, if you want the elapsed time between two arbitrary points - unlikesystem_clock
it won't be adjusted. But it's no good if you specifically want the time since the system started; there's nothing standard for that. – Aultman