How to callback function in game thread after FRunnableThread finish?

I have one task that need massive computation to compress. Whenever I execute this function in game thread, it will freeze the screen for about 5-10 seconds. Are there ways to execute this task on background so the game can still run instead of freezing? I am thinking of executing this task on separate thread. However, in that way, my game thread need to listen to task thread and execute next function after task thread finish. I can’t find any functions in FRunnable that do this. Is there “callback” function in FRunnable?

Take a look at Async() in Async.h… Something like: Async(EAsyncExecution::Thread, LambdaOrFunctionPointerToYourWork, LambdaOrFunctionPointerToYourCallback);

Alternatively, you can also roll your own FRunnable implementation. Pass a TFunction<> callback into your thread when you create it, and have the thread call this function when it’s done.

In either case, make sure your callback function is thread-safe, because it will be called from the worker thread. To forward it to the game thread, you could use AsyncTask(ENamedThreads::GameThread, …), or put it into a TQueue that will be processed in some object’s Tick, for example.

Note that the async stuff is still somewhat experimental, and the API may change in the not too distant future.

1 Like