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?
Asked
Active
Viewed 1.4k times
5
-
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 Answers
1
Basically you have 2 options imho:
- Create a second
thread
which will sleep instead of your mainthread
. - 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
0
Why not using a thread with a timer? Check this site: https://codereview.stackexchange.com/questions/40915/simple-multithread-timer