简体   繁体   English

Android上的C ++ 11 std :: chrono :: steady_clock问题

[英]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. 在我为Android平台制作的应用程序中,我一直在使用std :: chrono :: steady_clock进行区间计算

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 根据cppreference.com的定义

"Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward ." “类std :: chrono :: steady_clock代表一个单调时钟。 这个时钟的时间点不会随着物理时间的推移而减少 。”

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. 对稳定时钟的正式要求是对now()调用另一次调用now() 之前发生的结果总是小于或等于第二次调用的结果。 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 在Android上,AFAICT steady_clock与(来自Java)System.Clock.elapsedRealtime相同,在启动时重置为零 - https://developer.android.com/reference/android/os/SystemClock.html

I'm totally failing to dig up the source code for clock_gettime, though. 不过,我完全没有挖掘出clock_gettime的源代码。 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. https://android.googlesource.com/platform/ndk.git/+/43255f3d58b03cd931d29d1ee4e5144e86e875ce/sources/cxx-stl/llvm-libc++/libcxx/src/chrono.cpp#124显示它正在调用clock_gettime(CLOCK_MONOTONIC),但我'我不知道如何从那里穿透面纱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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