7
unsigned int Tick = GetTickCount();

This code is running only on Windows, but I want to use the C++ Standard library so it can run elsewhere.

I searched std::chrono, but I can't find a function like GetTickCount().

Do you know what I should use from std::chrono?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Joy Hyuk Lee
  • 756
  • 11
  • 22
  • Nothing to get the number of milliseconds since the system was started that I know of, but lots for relative times. – chris Jul 25 '14 at 13:59
  • Possible duplicate: http://stackoverflow.com/q/2958291/256138 – rubenvb Jul 25 '14 at 14:02
  • 3
    C++ doesn't have a notion of "the system started", so, no, there's nothing like it. There are plenty of things in `chrono` that might solve the same problem you're facing, though. We just need to know what problem you are facing. – R. Martinho Fernandes Jul 25 '14 at 14:03
  • 3
    @rubenvb: `clock_getttime()` is a POSIX standard interface, which isn't portable to Windows any more than `GetTickCount()` is portable to Linux, so the proposed duplicate is more 'related' than a duplicate. – Jonathan Leffler Jul 25 '14 at 14:09
  • `steady_clock` is the nearest equivalent, if you want the elapsed time between two arbitrary points - unlike `system_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. – Mike Seymour Jul 25 '14 at 14:42

2 Answers2

7

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
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
-2

I use chrono like this for the same purpose:

using namespace std::chrono;
duration_cast<milliseconds(high_resolution_clock::now().time_since_epoch()).count();