FAQ Database Discussion Community


Avoiding CPU Contention

c++,cpu,average,chrono
I have a program that I want to calculate its time of execution : #include <iostream> #include <boost/chrono.hpp> using namespace std; int main(int argc, char* const argv[]) { boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now(); // Intructions to burn time boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start; cout <<"---- time execution is " <<...

Cast chrono::milliseconds to uint64_t?

c++,c++11,chrono
Assuming I have the number of milliseconds in variable x: chrono::milliseconds x = std::chrono::duration_cast<chrono::milliseconds>(something); how do I convert x from chrono::milliseconds to uint64_t? I have tried: uint64_t num = std::chrono::duration_cast<uint64_t>(x); but it says: no matching function for call to duration_cast(std::chrono::milliseconds&) ...

Is it possible to restrict a member function from accessing a member variable?

c++,multithreading,thread-safety,chrono,stdatomic
I have a thread class similar to this: class thr { void run() { for (;;) { // block on a queue // do some processing ++loops_; } } void get_metrics(int& qps) { std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now(); double delta = std::chrono::duration<double>(now - last_metrics_time_).count(); qps = std::round(loops_ / delta); loops_ =...

timing a function call as if-statement condition in cpp

c++,if-statement,timing,chrono
I have a function and sometimes it gets called as a condition in if-statements, and I am interested to time exactly these calls. I wonder if there is any way to do something like this for timing it in cpp: using watch = std::chrono::high_resolution_clock; std::chrono::nanoseconds time(0); if ( auto start...

Convert chrono duration to time_point

c++11,chrono
How can I convert a chrono duration to a time_point, which is later than clock's epoch with the given duration? I tried to find epoch time in chrono clock without success.

C++ condition variable wait_for not behaving as expected

c++,c++11,condition-variable,chrono
I am having a problem understanding why a test case that I think should be passing is failing most of the time. I have distilled the test down to the condition variable and using the wait_for method, specifically testing it to see if it does indeed wait for at least...

How to run a clock on its own thread

c++,multithreading,c++11,chrono
I'm trying to create a step sequencer in C++ that will eventually send out MIDI data. I created it by having a clock on its own thread that calculates the amount of time since the last beat, and if it is time for the next beat, it writes a piece...

std thread sleep_for doesnt work with some chrono::duration

c++,c++11,chrono,stdthread
I am using VS2012 and I have problem with following example: #include <chrono> #include <thread> int main() { // doesn't compile and I don't understand why: std::this_thread::sleep_for(std::chrono::duration<double>(0.1)); // I can use this but still I would like to know the reason: std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(100)); return 0; } Both duration should...

Running a function for specified duration : C++ with

c++,c++11,chrono
I need to implement a function that should do specified task for specified duration which is passed to it as parameter (std::chrono::milliseconds). I have come up with code : void Run(std::chrono::milliseconds ms) { std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; while (elapsed_seconds <= (ms /...

Why does coliru return the same values for chrono::system_clock::now().time_since_epoch()?

c++,c++11,chrono,coliru
While testing someone else's code on coliru, I noticed that std::chrono::system_clock::now().time_since_epoch() returns the same values on multiple runs. I tested the following code with g++ and clang++ on coliru. #include <iostream> #include <chrono> int main() { auto seed = std::chrono::system_clock::now().time_since_epoch().count(); std::cout << seed << "\n"; std::cout << std::chrono::system_clock::now().time_since_epoch().count() << "\n";...

C++ chrono epoch is 100000 times smaller?

c++,epoch,chrono
THe code below returns number like 14517044: typedef std::chrono::high_resolution_clock Clock; typedef std::chrono::milliseconds Milliseconds; unsigned long res = std::chrono::duration_cast<Milliseconds>(Clock::now().time_since_epoch()).count(); printf("epoch %lu", res); But http://www.epochconverter.com/ site shows numbers like 1426143327, where as it is in seconds. So the epoch with Milliseconds has 8 digits and epoch with seconds has 10 digits? What...

Can't verify the conversion from time_point to tm and tm back to time_point

c++,std,chrono,ctime
I created a current time_point and converted it to structure tm and printed it's values. Now converted this tm structure to time_point. On comparing the first and second time_points, it is telling that they're different. But the values of structure are exactly same. Can someone spot, where I'm doing wrong?...

Is There an Upper Bound in my locale for Time Related Information?

c++,boost,chrono,ctime,boost-locale
Is there a definition somewhere in the standard namespace that sets forward: Months in a year Days in a week Hours in a day Minutes in an hour Seconds in a minute The struct tm has contains member variables that must be in these ranges, but I can't find the...

C++ chrono library on Raspberry Pi

c++,raspberry-pi,c++14,chrono
On a Raspberry Pi 2, I need to call a php file on a regular basis, typically every 100ms. I found this c++ code which looks like it does what I need and a test version of it compiles and runs fine using CodeBlock on Windows. I've updated the wheezy...

Is there a way to assign zero to std::chrono::nanoseconds

c++,c++11,chrono
Is there a way to assign zero to a duration of type std::chrono::nanoseconds? I tried duration::zero but it failed.

How to use `duration_cast` for a derived class?

c++,c++11,chrono
I'm cleaning up my timer class using std::chrono. Everything else went smooth except that I cannot seem to apply duration_cast to derived classes. Well, I made it work in a different way, but I'd still like to know what I missed. See here for the error message. #include <iostream> #include...

DateTime to UTC

c++,boost,chrono
What is the proper way to convert date time values to UTC in milliseconds? I've been looking into std::chrono and boost without really finding a pragmatic way to accomplish this, any ideas? i.e. auto local_time_to_utc_milliseconds(int year, int month, int day, int hour, int minute, int second, int millisecond) -> std::uint64_t...

Convert double to time_t

c++,time,standards,chrono,time-t
I have a double containing seconds. I would like to convert this into a time_t. I can't find a standard function which accomplishes this. Do I have to fill out the time_t by hand?...