Execute function in C ++ after delay without using sleep

Is there an easy way to do something after a certain delay in C ++? In python, I just use frame.after (ms, myFunction) which is nice and lightweight - it seems like a harder problem in C ++! Earlier it was suggested that I use Sleep (), unfortunately this does not work as I am writing a mod for Half-Life 2 and if I use Sleep then the whole game hangs for X seconds and not just raises an event after X seconds. So is there any other way to call the function after a certain delay without using sleep?

+3


source to share


2 answers


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



0


source


Basically you have 2 options imho:

  • Create a second thread

    one that will sleep instead of the main one thread

    .
  • Create a second thread

    one that contains a timer.

I can only recommend raising io_service

You might want to go through all the timer tutorials if you are new to development.




If you would like to stay with std

, check this link for more information onstd::async

And this for usage guidestd::threads

0


source







All Articles