# Is is safe to call "trace" methods from another thread?

**URL:** <https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 24, 2019, 5:39pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950 "2019-06-24T17:39:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scha](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scha/32/1135430_2.png) [@scha](https://forums.unrealengine.com/u/scha)\
**Post date:** [June 24, 2019, 5:39pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950/1 "2019-06-24T17:39:53Z")

</div>

Is it safe to call trace functions, like OverlapMultiByObjectType, from another thread (not the game thread)?  
I’ve put my traces in an AsyncTask and the speedup is pretty awesome, but is that safe or I’m just being lucky?

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [June 24, 2019, 6:29pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950/2 "2019-06-24T18:29:16Z")

</div>

No, you have to use the AsyncPhysics interface, for example “UWorld::AsyncLineTraceByChannel()”:

> **[UWorld::AsyncLineTraceByChannel](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Engine/UWorld/AsyncLineTraceByChannel/)**
>
> Interface for Async.

---

<div class="post-metadata">

**Author:** ![scha](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scha/32/1135430_2.png) [@scha](https://forums.unrealengine.com/u/scha)\
**Post date:** [June 24, 2019, 7:15pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950/3 "2019-06-24T19:15:52Z")

</div>

> [@BrUnO\_XaVIeR](#):
>
> No, you have to use the AsyncPhysics interface, for example “UWorld::AsyncLineTraceByChannel()”:
> 
> [https://docs.unrealengine.com/en-US/API/Runtime/Engine/Engine/UWorld/AsyncLineTraceByChannel/index.html](https://docs.unrealengine.com/en-US/API/Runtime/Engine/Engine/UWorld/AsyncLineTraceByChannel/index.html)

Could you please explain a little bit? I looked in the source and it seems it’s a FPhysicsInterface call executed inside an async task plus some threadsafe communication.

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [June 24, 2019, 7:20pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950/4 "2019-06-24T19:20:09Z")

</div>

They return safe struct copy containing data from the alien thread.  
Normal functions might return pointers to world elements or delegates and so they run in game thread.

---

<div class="post-metadata">

**Author:** ![scha](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scha/32/1135430_2.png) [@scha](https://forums.unrealengine.com/u/scha)\
**Post date:** [June 24, 2019, 7:24pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950/5 "2019-06-24T19:24:39Z")

</div>

You mean the normal function, like OverlapMultiByObjectType, can return pointers to Components that are not in the same World? I do many traces but only return one component back to the game thread.

edit: i mean, I just give some actors to the task, do some traces and calculation in the task, then give back a component. They are all supposed to be in the same world. So, if the communication between threads is okay, the trace itself is is tread-safe?

Let’s say the task and the game thread are not communicating, the task runs traces and print numbers on the log. Is that safe?

---

<div class="post-metadata">

**Author:** ![IrSoil](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IrSoil](https://forums.unrealengine.com/u/IrSoil)\
**Post date:** [December 26, 2024, 2:03pm UTC](https://forums.unrealengine.com/t/is-is-safe-to-call-trace-methods-from-another-thread/127950/6 "2024-12-26T14:03:11Z")

</div>

While i’m still looking for answer, it’s worth mention that this suggestion doesn’t seems correct now. I’m on 5.3 and `UWorld::AsyncLineTraceByChannel` calls the `StartNewTrace` that has a guard against threading at the very begining:

```c++
		// Using async traces outside of the game thread can cause memory corruption
		check(IsInGameThread());

```

upd:  
`ActiveSound.cpp` Have an interesting comment at the line 1220

```c++
				if (UWorld* WorldPtr = World.Get())
				{
					// LineTraceTestByChannel is generally threadsafe, but there is a very narrow race condition here
					// if World goes invalid before the scene lock and queries begin.
					bIsOccluded = WorldPtr->LineTraceTestByChannel(SoundLocation, ListenerLocation, OcclusionTraceChannel, Params);
				}

```

So it seems default traces is a way to go now. At least they seems to work at the first glance.  
Also there is a topic with an engine fix that seems relevant to the problem, but i’m don’t need it yet.

> [@How to do scene sweeps/traces on the async physics thread?](https://forums.unrealengine.com/t/how-to-do-scene-sweeps-traces-on-the-async-physics-thread/607030/2):
>
> Found two solutions, posting here so others can do the same. Basically the problem is in the function GetThreadQueryContext() in SceneQuery.cpp. On line 330 it says “//TODO: need a way to know we are on a physics thread task (this isn’t supported yet)”. Solution one is to replace the line below that comment with the following: if (IsInGameThread()) { return EThreadQueryContext::GTData; } else { return EThreadQueryContext::PTOnlyData; } The above assumes that any other thread th…
