---
title: "[UE4]如何跨线程访问GameThread中的数据"
author: "aigo"
date: "2016-05-07T15:43:39Z"
tags: []
categories: ["开源软件"]
slug: "ue4-gamethread"
draft: false
lastmod: "2016-05-08T19:20:28Z"
id: 2296553
url: "https://aigo.iteye.com/blog/2296553"
---

UE4中如果在自己创建的FRunnable线程中取访问引擎相关的API，比如你想在非GameThread中创建UI，运行时UE4时会给assert错误：

Assertion failed: **IsInGameThread**() \[File:D:\\BuildFarm\\buildmachine\_++UE4+Release-4.11\\Engine\\Source\\Runtime\\Slate\\Public\\Framework\\Application\\SlateApplication.h] \[Line: 156] 

 

如何跨线程开访问UE4的API？

看了两个帖子，都说只能搞个queue，自定义线程往queue塞数据，Tick函数中对这个queue读取。因为GameThread中的数据不是线程安全的，所以只能通过这种方式。

 

自己创建的线程哪些不能做？官方文档上的回答：

[https://wiki.unrealengine.com/Multi-Threading:\_How\_to\_Create\_Threads\_in\_UE4#What\_Not\_to\_Do](https://wiki.unrealengine.com/Multi-Threading:_How_to_Create_Threads_in_UE4#What_Not_to_Do)

- Do not try to modify, create, or delete UObjects from other threads!

You can prepare all the data / do all the calculations, but only the game thread should be actually spawning / modifying / deleting UObjects / AActors.

- Dont try to use TimerManager outside of the game thread :)

<!--THE END-->

- Don't try to draw debug lines/points etc, as it will likely crash, ie DrawDebugLine(etc...)

 

参考：

**How do I execute code on the Game thread from an FRunnable thread?**

[https://answers.unrealengine.com/questions/262585/how-do-i-execute-code-on-the-game-thread-from-an-f.html](https://answers.unrealengine.com/questions/262585/how-do-i-execute-code-on-the-game-thread-from-an-f.html)

**Thread safety, delegates and bPostTickComponentUpdate**

[https://answers.unrealengine.com/questions/30413/thread-safety-delegates-and-bposttickcomponentupda.html](https://answers.unrealengine.com/questions/30413/thread-safety-delegates-and-bposttickcomponentupda.html)