# Executing and passing data between threads using a delegate call?

**URL:** <https://forums.unrealengine.com/t/executing-and-passing-data-between-threads-using-a-delegate-call/111661>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 1, 2018, 7:01pm UTC](https://forums.unrealengine.com/t/executing-and-passing-data-between-threads-using-a-delegate-call/111661 "2018-06-01T19:01:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Whammy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Whammy](https://forums.unrealengine.com/u/Whammy)\
**Post date:** [June 1, 2018, 7:01pm UTC](https://forums.unrealengine.com/t/executing-and-passing-data-between-threads-using-a-delegate-call/111661/1 "2018-06-01T19:01:58Z")

</div>

I have a UDP socket class that listens for outside server queries. The problem is that the engine UDP socket listener seems to be running in a different thread from the main thread. It’s not clear what’s happening, but I’m getting chaotic results as a result. In short, the UDP listener (internal to the engine) is calling my receiver delegate with the packet data, which in turn calls a second delegate which parses the received packet for display on the UI. The data is being passed using a TSharedPtr\<\> to pass the received packet between delegates. However, I imagine there’s some thread hopping going on here, so I’m fuzzy on the safety of passing the packet between delegates and even as to which thread the delegates are getting executed in. Could someone offer some insight to this?

---

<div class="post-metadata">

**Author:** ![Manoel.Neto](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/manoel.neto/32/1210013_2.png) [@Manoel.Neto](https://forums.unrealengine.com/u/Manoel.Neto)\
**Post date:** [June 2, 2018, 1:32am UTC](https://forums.unrealengine.com/t/executing-and-passing-data-between-threads-using-a-delegate-call/111661/2 "2018-06-02T01:32:55Z")

</div>

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

```auto

AsyncTask(ENamedThreads::GameThread, =]()
{    
    // Code placed here will execute on game thread
});

```

Remember to include “Async.h” in your .cpp

---

<div class="post-metadata">

**Author:** ![spacebares](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@spacebares](https://forums.unrealengine.com/u/spacebares)\
**Post date:** [August 18, 2019, 10:24pm UTC](https://forums.unrealengine.com/t/executing-and-passing-data-between-threads-using-a-delegate-call/111661/3 "2019-08-18T22:24:19Z")

</div>

> [@Manoel.Neto](#):
>
> [A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4#What_Not_to_Do)
> 
> ```auto
> 
> AsyncTask(ENamedThreads::GameThread, =]()
> {
> // Code placed here will execute on game thread
> });
> 
> ```
> 
> Remember to include “Async.h” in your .cpp

too slow

Please enter a message with at least 10 characters (please\_enter\_message\_x\_chars)

---

<div class="post-metadata">

**Author:** ![Shmoopy1701](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shmoopy1701/32/1194536_2.png) [@Shmoopy1701](https://forums.unrealengine.com/u/Shmoopy1701)\
**Post date:** [August 20, 2019, 5:14pm UTC](https://forums.unrealengine.com/t/executing-and-passing-data-between-threads-using-a-delegate-call/111661/4 "2019-08-20T17:14:13Z")

</div>

I’ve shipped 4 Unreal products with C++ game code TCP connection to another process or PC.

Basically you just:

- spin up your TCP server or client as usual and keep references in GameInstance so it persists through level loads
- in Unreal … only read incoming messages and farm them out to World objects during GameMode.Tick()
- sending can be done at anytime
