2

I wonder if there is a way to calculate elapsed time in c++, without blocking execution and if possible avoiding thread.

Situation: a bomb is created and need to explode 5 seconds later. Can we avoid to create a thread who will sleep 5 sec until explosion then kill himself?

  • 1
    You should rename your question to something like - sleep for some time without blocking. AFAIK, there is no way to do this without threads. – Nemanja Boric Apr 30 '13 at 14:22
  • 1
    Ok thanks, we were 98% sure their was no solutions to this but we had to ask. Thanks Nemanja –  Apr 30 '13 at 14:27
  • 2
    @Mayerz: Well if you just have to count the time elapsed, you can use [std::chrono::system_clock](http://en.cppreference.com/w/cpp/chrono/system_clock) to get the time before then after the execution of some piece of code and compute the difference to get the elapsed time. But is that what you are asking for ? – ereOn Apr 30 '13 at 14:31
  • You could also take a look as Boost.Asio, maybe something like http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/deadline_timer.html – Philipp Aumayr Apr 30 '13 at 14:33
  • 1
    With OS specific functions or external libraries this is certainly possible. An example is to use a timer of the ``boost-asio`` library – jcm Apr 30 '13 at 14:33
  • Depends on what you're actually doing and what precision you need - if time's not too crucial you can do other stuff and check regularly how much time has passed. Of course, that requires you to be able to insert checks at reasonable places. For instance, if you were making a game, you'd check your timers every frame. – molbdnilo Apr 30 '13 at 14:36
  • Or just call posix `alarm()`, which will call-back a signal handler after the specified time. See here for something similar on Windows: http://stackoverflow.com/questions/4511732/alarm-function-on-linux-and-windows-cant-find-a-equivalent-for-windows-c – Wandering Logic Apr 30 '13 at 15:29
  • I can't use Boost, just the STL's functions. –  May 02 '13 at 11:11

2 Answers2

2

You can have non-blocking time counter, simply call it from time to time in the middle of execution of your routine actions. As soon as there is timeout - do what you need. This way no additional thread required.

#include <ctime>

// start counter with current second
time_t counter = time(0);

// we need to wait 5 seconds
time_t timeout = 5;

bool exit = false;

void handle_timeout()
{
    exit = true;
}

void func()
{
    while(!exit)
    {
        // do something

        if (time(0) - counter > timeout)
            handle_timeout();

        // yeild() or sleep(0) can be put here, to avoid 100% cpu load in case of high load
    }
}

int main()
{
    func();
}
Bogolt
  • 520
  • 1
  • 3
  • 9
  • Ok, the sleep(0) appear to be the solution for non 100% cpu load. Will wait some more comments but you are actually the winning answer! –  May 02 '13 at 11:08
1

Using std::chrono you could use a non-blocking elapsed timer in the following manner

call this at the beginning of the desired time measurement:

std::chrono::steady_clock::time_point begin_time = std::chrono::steady_clock::now();

call this at the end of the desired time measurement:

std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now(); 

compute the elapsed time:

std::chrono::steady_clock::time_point elapsed = end_time - begin_time;

If lower resolution is desired such as minutes or hours, std::chrono can be adapted using std::chrono::steady_clock::minutes(/*num minutes*/) or `std::chrono::steady_clock::hours(/*num hours*/)

Mushy
  • 2,535
  • 10
  • 33
  • 54
  • This is ok, I knew about this method. But if I use this in my loop, it will be like an infinite loop during the time required for the action (virtual time of the action, for example 5 sec if I will it, because the action is instant) I hope you understood me. –  May 02 '13 at 11:05
  • You need only make two calls within your code; first to retrieve current time and then one more call to retrieve the end time. When ready to discover the elapsed time, subtract end from beginning and there you have it. – Mushy May 03 '13 at 12:28