endail/hx711

high_resolution_clock should be changed to steady_clock

endail opened this issue · 0 comments

hx711/src/HX711.cpp

Lines 310 to 328 in 42d6305

bool HX711::waitReady(const std::chrono::nanoseconds timeout) const {
using namespace std::chrono;
const auto maxEnd = high_resolution_clock::now();
while(true) {
if(this->isReady()) {
return true;
}
if(high_resolution_clock::now() >= maxEnd) {
return false;
}
}
}

The high_resolution_clock is not implemented consistently across different standard library implementations, and its use should be avoided. It is often just an alias for std::chrono::steady_clock or std::chrono::system_clock, but which one it is depends on the library or configuration. When it is a system_clock, it is not monotonic (e.g., the time can go backwards). For example, for gcc's libstdc++ it is system_clock, for MSVC it is steady_clock, and for clang's libc++ it depends on configuration.

Generally one should just use std::chrono::steady_clock or std::chrono::system_clock directly instead of std::chrono::high_resolution_clock: use steady_clock for duration measurements, and system_clock for wall-clock time.

https://en.cppreference.com/w/cpp/chrono/high_resolution_clock