简体   繁体   中英

C++11 std::chrono::steady_clock issue on Android

I have been using std::chrono::steady_clock for interval calculation in an application i am making for Android platform.

Code:

// On application start
auto timeSinceEpoch = std::chrono::steady_clock::now().time_since_epoch();

auto timeInSec = std::chrono::duration_cast<seconds>(timeSinceEpoch).count();

log("On Enter Start Time Point - %lld", timeInSec);

Output:

On Enter Start Time Point - 521

Now i switch off the phone and restart the phone. I run my application and this time Output is:

On Enter Start Time Point - 114

As per definition at cppreference.com

"Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward ."

How is the output when i restart the phone giving lesser value?

If anyone has faced this issue please help me out here. Thanks!!

The formal requirement for a steady clock is that the result of a call to now() that happens before another call to now() is always less than or equal to the result of the second call. The happens before relationship only applies to actions within a program run. A steady clock is not required to be steady across different invocations of a program.

On Android, AFAICT steady_clock is the same as (from Java) System.Clock.elapsedRealtime, which resets to zero on boot -- https://developer.android.com/reference/android/os/SystemClock.html

I'm totally failing to dig up the source code for clock_gettime, though. https://android.googlesource.com/platform/ndk.git/+/43255f3d58b03cd931d29d1ee4e5144e86e875ce/sources/cxx-stl/llvm-libc++/libcxx/src/chrono.cpp#124 shows it calling clock_gettime(CLOCK_MONOTONIC), but I'm not sure how to penetrate the veil from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM