Component's bRunOnAnyThread has no effect

I’m trying to run a component’s tick in a background thread.

To test if bRunOnAnyThread was working, I simply added

void UmyComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Sleep(200);
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}

The sleep function locks up the entire game, and makes it unplayable. If i’m doing work in a background thread, the game thread should not be affected!

What can I do to fix this?

Hi ,

Sorry for the delayed response. Would you be able to elaborate a bit more on what you are trying to accomplish? Setting bRunOnAnyThread to true allows parallel processing of tick functions, but it is not necessarily UObject safe. One of the biggest hurdles to overcome is that everything in a specific TickGroup needs to finish its process before the next TickGroup can begin, so adding a lengthy process to one element in a TickGroup will result in everything locking up while that process completes, regardless of what thread it is running on.

What you may want to do is create a new thread in your Component’s code, start whatever you need to have done there, then report the results back to the Component using events once it has been completed. If you need more than the basic threading the Engine uses by default, unfortunately it generally will require more setup than just setting bRunOnAnyThread to true.

1 Like

This makes sense, thank you.