chrono

Should std::chrono::steady_clock::now be noexcept?

自闭症网瘾萝莉.ら 提交于 2020-01-05 05:57:33
问题 I've noticed that std::chrono::steady_clock::now has the noexcept specifier in the documentation at cplusplus.com. However, I haven't found any provision for this in the latest C++11 draft (unfortunately I don't have a copy of the standard). Is it a mistake in the cplusplus.com documenation or should std::chrono::steady_clock::now have the noexcept specifier? 回答1: § 20.11.7.2 of the C++11 standard's definition of steady_clock : class steady_clock { public: typedef unspecified rep; typedef

sleep_until a timepoint in the past

时光毁灭记忆、已成空白 提交于 2020-01-04 06:29:09
问题 I have what I thought was a simple pattern - I want to make a timepoint 5 seconds in the future, run a task which might take a while, and then sleep until that timepoint (potentially not sleeping at all if that time has already been reached.) However, whenever trying to use std::this_thread::sleep_until with a timepoint in the past, my application instead hangs forever. Here is an MCVE: #include <chrono> #include <thread> int main(){ std::this_thread::sleep_until(std::chrono::steady_clock:

What is the proper way of seeding std::mt19937 with std::chrono::high_resolution_clock inside a class?

戏子无情 提交于 2020-01-03 15:39:49
问题 First off, hello everyone! This is my first ever question here, so I hope I am not screwing up. I googled a lot before writing here. I am new to coding, to c++ and I am learning it on my own. Considering that I was told that it is a good practice (i'm probably wrong here) to only seed any Random Engine once, what is the proper / best / more efficient way of using std::mt19937 from the random standard library inside a class, seeded by std::chrono::high_resolution_clock::now().time_since_epoch(

What is the proper way of seeding std::mt19937 with std::chrono::high_resolution_clock inside a class?

◇◆丶佛笑我妖孽 提交于 2020-01-03 15:38:09
问题 First off, hello everyone! This is my first ever question here, so I hope I am not screwing up. I googled a lot before writing here. I am new to coding, to c++ and I am learning it on my own. Considering that I was told that it is a good practice (i'm probably wrong here) to only seed any Random Engine once, what is the proper / best / more efficient way of using std::mt19937 from the random standard library inside a class, seeded by std::chrono::high_resolution_clock::now().time_since_epoch(

C++ conversion operator to chrono::duration - works with c++17 but not C++14 or less

微笑、不失礼 提交于 2020-01-03 08:35:30
问题 The following code compiles with gcc 7.1.0 with C++17 set but does not compile with C++14 set (or Visual Studio 2017). It is easy to reproduce on Wandbox. What has to be done to make it work with C++11/14? #include <iostream> #include <chrono> int main() { struct Convert { operator std::chrono::milliseconds() { std::cout << "operator std::chrono::milliseconds" << std::endl; return std::chrono::milliseconds(10); } operator int64_t () { std::cout << "operator int64_t" << std::endl; return 5; }

Android NDK chrono epoch is not correct (std::chrono::high_resolution_clock)

风格不统一 提交于 2020-01-03 07:15:54
问题 The code below does not print epoch. typedef std::chrono::high_resolution_clock Clock; typedef std::chrono::milliseconds Milliseconds; auto res = std::chrono::duration_cast<Milliseconds>(Clock::now().time_since_epoch()).count(); std::stringstream ss; ss << res; printf(">>>>>>>>>>> TimeUtiles::getTimestamp %s", ss.str().c_str()); I use NDK r9d and selected NDK toolchain version was 4.8 ! EDIT: Changed std::chrono::high_resolution_clock to std::chrono::system_clock and it worked. Why? 回答1:

Android NDK chrono epoch is not correct (std::chrono::high_resolution_clock)

蹲街弑〆低调 提交于 2020-01-03 07:15:10
问题 The code below does not print epoch. typedef std::chrono::high_resolution_clock Clock; typedef std::chrono::milliseconds Milliseconds; auto res = std::chrono::duration_cast<Milliseconds>(Clock::now().time_since_epoch()).count(); std::stringstream ss; ss << res; printf(">>>>>>>>>>> TimeUtiles::getTimestamp %s", ss.str().c_str()); I use NDK r9d and selected NDK toolchain version was 4.8 ! EDIT: Changed std::chrono::high_resolution_clock to std::chrono::system_clock and it worked. Why? 回答1:

Can compiler reorder code over calls to std::chrono::system_clock::now()?

∥☆過路亽.° 提交于 2020-01-02 03:26:09
问题 While playing with VS11 beta I noticed something weird: this code couts f took 0 milliseconds int main() { std::vector<int> v; size_t length =64*1024*1024; for (int i = 0; i < length; i++) { v.push_back(rand()); } uint64_t sum=0; auto t1 = std::chrono::system_clock::now(); for (size_t i=0;i<v.size();++i) sum+=v[i]; //std::cout << sum << std::endl; auto t2 = std::chrono::system_clock::now(); std::cout << "f() took " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << "

Chrono Timer Not Converting Seconds Properly

会有一股神秘感。 提交于 2020-01-02 00:10:20
问题 I am having an interesting, yet strange issue with my game timer. It seems like the milliseconds works just fine. However, when I try to apply the std::chrono::seconds cast I suddenly get 0.000000 when casting to a float. My timer is as follows: #include <iostream> #include <time.h> #include <chrono> class Timer { public: typedef std::chrono::high_resolution_clock Time; typedef std::chrono::milliseconds ms; //<--If changed to seconds, I get 0.00000 typedef std::chrono::duration<float> fsec;

Using 'auto' type deduction - how to find out what type the compiler deduced?

醉酒当歌 提交于 2019-12-31 08:28:26
问题 How can I find out what type the compiler deduced when using the auto keyword? Example 1: Simpler auto tickTime = 0.001; Was this deduced as a float or a double? Example 2: More complex (and my present headache): typedef std::ratio<1, 1> sec; std::chrono::duration<double, sec > timePerTick2{0.001}; auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2; What type is nextTickTime ? The problem I'm having is when I try to send nextTickTime to std::cout . I get the