5

is there a simple way of making something happen after a specific delay in C++? In python, I just use frame.after(ms,myFunction) which is nice and easy - it seems like a far trickier problem in C++! It has previously been suggested that I use Sleep(), unfortunately this doesn't work as I am writing a mod for Half-Life 2 and if I use Sleep then the whole game hangs for X seconds rather than just calling the event after X seconds. So is there another way to call a function after a specific delay without using sleep?

psychojim
  • 59
  • 1
  • 1
  • 4
  • 2
    [`std::async()`](http://en.cppreference.com/w/cpp/thread/async) with `std::this_thread::sleep()`? Is that acceptable? – The Paramagnetic Croissant Nov 06 '14 at 22:11
  • Assuming that `frame.after(ms, myFunction)` is part of the Python API for Half-Life mods, then there must be a C++ equivalent, and that is what you want. – zwol Nov 06 '14 at 22:22

2 Answers2

1

Basically you have 2 options imho:

  1. Create a second thread which will sleep instead of your main thread.
  2. Create a second thread which contains a timer.

I can only recommend boosts io_service

You might want to go through all the timer tutorials if you're new to boost.


If you want to stay with the std check this link for more information on std::async

And this for a guide on how to use std::threads

Community
  • 1
  • 1
deW1
  • 5,562
  • 10
  • 38
  • 54
0

Why not using a thread with a timer? Check this site: https://codereview.stackexchange.com/questions/40915/simple-multithread-timer

Community
  • 1
  • 1
Rdey
  • 420
  • 6
  • 9