I need to know how create a timer or measure out 500ms in C++ in a linux environment. I have tried using gettimeofday and using the time structure but cant get the correct precision for milliseconds. What I am trying to do is have an operation continue for a max of 500ms…after 500ms something else happens.
Share
If you have access to C++11 then your best bet it to use std::chrono library
http://en.cppreference.com/w/cpp/chrono/duration
I aren’t entirely sure what you want to do with it do you want to wait for exactly 500ms?
you can so this for that
you can do an operation until 500 milliseconds has elapsed by getting a time pointer and check to see whether timepoint – system_time::now() is greater than 500ms
If you don’t have C++11 this will also work with boost chrono library. The advantage of this approach is that it is portable unlike using linux time functions.