Is UE_LOG thread safe?

I would like to call UE_LOG from another thread (spawned using C++11 std::thread, not an FRunnableThread).

Is UE_LOG thread safe?

Also, I read on the wiki that you shouldn’t call some unreal objects from a separate thread:

Is it safe to call UE_LOG?

Thanks

+1 for this, curious. I’ve generally found that calling UE_LOG from different threads gives an expected response (make sure the variables you bind to UE_LOG are safely accessed though or copied), but it would be good to get some answer on what the best practices are.

2 Likes

Yes, I’m new to unreal and so am very interested in best practices! :slight_smile: Interestingly, if it is thread safe, then that would also mean it’s probably best to avoid it in performance critical code, since it would likely compete with other threads for a mutex.

The answer is “it depends.” Each FOutputDevice can overload CanBeUsedOnAnyThread() to indicate whether it’s threadsafe or not. Typically if it’s expected to be called from a particular thread it will assert with check(AllowedThreadId == GetThreadId());

1 Like

As of 4.25,
CanBeUsedOnAnyThread() is true for these FOutputDevices:

  • SDeviceOutputLog
  • FWindowsConsoleOutputDevice
  • FOutputDeviceAnsiError
  • FOutputDeviceDebug
  • FOutputDeviceFile
  • FOutputDeviceMemory
  • FOutputDeviceStdOutput

false otherwise.

4 Likes