How does std :: chrono :: stable_clock :: now report errors?

This is related to my previous question , where I asked if std::chrono::steady_clock::now should be noexcept . Now that I know I'm curious how this function reports errors? For example, a general Linux implementation of this function uses clock_gettime , which may return an error.

+4
source share
2 answers

All errors that can be reported by Linux clock_gettime are not possible in the debugged implementation of std::chrono::steady_clock::now() .

Errors:

 int clock_gettime(clockid_t clk_id, struct timespec *tp); EFAULT 

tp points out of the available address space.

In std::chrono::steady_clock::now() no user tp that is not provided to the user. If std::chrono::steady_clock::now() passes the wrong tp to clock_gettime , this is just a steady_clock error that is easily fixed.

 EINVAL 

The specified clk_id is not supported on this system.

In std::chrono::steady_clock::now() no user clk_id . If std::chrono::steady_clock::now() passes the wrong clk_id to clock_gettime , this is just a steady_clock error that needs to be fixed.

Please note that the steady_clock implementation steady_clock not guaranteed to be portable and can be ported to any given platform and checked for correctness. It may turn out that steady_clock cannot be implemented in terms of clock_gettime on some platforms.

 EPERM 

clock_settime () is not allowed to set the specified hours.

Not applicable to steady_clock since there is no API to set the time.

+7
source

This is not true. There are no error messages in the API. Thus, the Linux implementation must either handle errors internally or not return (for example, through exit() or std::terminate() ).

+1
source

Source: https://habr.com/ru/post/1495797/


All Articles