To reproduce, on emscripten 1.36.5 (incoming branch)
(1) here is simple test case (c.cpp)
#include <chrono>
#include <iostream>
int
main()
{
std::cout << "before" << std::endl;
auto now = std::chrono::high_resolution_clock::now();
std::cout << "after" << std::endl;
return 0;
}
(2) Compile it using
(3) In generated c.js, add following line as first line (To simulate Safari/IE/Edge webworker environment restriction on FF/Chrome)
Output:
Only before is printed followed by exception (some implementations of boost::chrono::steady_clock::now may throw exception; but here we are using std::chrono )
Reason:
__ZNSt3__26chrono12steady_clock3nowEv (_std::__2::chrono::steady_clock::now()) calls (via invoke_ii) _clock_gettime(clk_id =1,tp)
_clock_gettime calls_emscripten_get_now_is_monotonic which return false
_clock_gettime then sets error number and return -1.
__ZNSt3__26chrono12steady_clock3nowEv on seeing this error throws if exceptions are enabled or continue (with unexpected time value) if exceptions are disabled.
YES, _emscripten_get_now checks if performance exists and then uses a fallback but it is not called due to monotonicity test failure
Other consideration:
As mentioned in comment, Date.now does not guarantee monotonicty and it may be issue depending on some implementation. An answer here indicate how Date.now can be forced to be monotonic: