Delayed execution in C++

Unity allows your to delay the execution of a function by using something like this: Invoke(“DoSomething”, 2);

DoSomething will be called after 2 seconds. I can call Invoke inside Update(), Awake() or any other callback.

You could launch another thread that waits two seconds, executes the function and stops. There’s many ways to do that.

What’s the “Urho” way?

I haven’t looked into it that much but it looks like Urho3D has it’s own way of doing it instead of using c++11 threads.

Yeah, but it seems to be just a task queue. WorkItem does not contain any delay info, or the WorkQueue. You can just add new WorkItem’s to the WorkQueue, no scheduling.

There appears to also be a Sleep function in the Timer class that you could call.

If all else fails you could just use c++11’s chrono and thread headers for multithreading and do this_thread sleep_for :wink:

How about creating a scheduler that you add tasks to. A task holds the invoke time and a delegate to a method or a function. Subscribe to E_UPDATE for example and on every tick check if a task is to be executed.

1 Like