Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

執行緒安全的 API

執行緒

執行緒用於平衡多核心 CPU 的運算效能。Godot 支援多執行緒,但不是整個引擎都支援。

以下列出 Godot 不同範疇中可用多執行緒的方式。

全域作用域

Most Global Scope singletons are thread-safe by default. Accessing servers from threads is supported. However, for the rendering and physics servers, thread-safe operation must be enabled in the project settings first.

This makes singletons ideal for code that creates dozens of thousands of instances in servers and controls them from threads. Of course, it requires a bit more code, as this is used directly and not within the scene tree.

場景樹

Interacting with the active scene tree is not thread-safe. Make sure to use mutexes when sending data between threads. If you want to call functions or set properties from a thread, you may use call_deferred or set_deferred:

# Unsafe:
node.add_child(child_node)
# Safe:
node.add_child.call_deferred(child_node)

但在現有場景樹之外建立場景片段(以樹狀排列的節點)是沒問題的。如此一來,可以在執行緒中建立或產生部份場景,再於主執行緒新增進場景樹:

var enemy_scene = load("res://enemy_scene.scn")
var enemy = enemy_scene.instantiate()
enemy.add_child(weapon) # Set a weapon.
world.add_child.call_deferred(enemy)

Still, this is only really useful if you have one thread loading data. Attempting to load or create scene chunks from multiple threads may work, but you risk resources (which are only loaded once in Godot) being tweaked by the multiple threads, resulting in unexpected behaviors or crashes.

只有當你*非常*確定自己操作無誤,且能保證同一資源不會被多個執行緒同時存取或設定時,才建議使用多個執行緒來產生場景資料。否則,直接用伺服器 API(完全執行緒安全),避免直接操作場景或資源會更安全。

算繪

Instancing nodes that render anything in 2D or 3D (such as Sprite2D or MeshInstance3D) is not thread-safe by default. To run the rendering driver on a separate thread, set the Rendering > Driver > Thread Model project setting to Separate.

Note that the Separate thread model has several known bugs, so it may not be usable in all scenarios.

警告

You should avoid calling functions involving direct interaction with the GPU on other threads, such as creating new textures or modifying and retrieving image data. These operations can lead to performance stalls because they require synchronization with the RenderingServer, as data needs to be transmitted to or updated on the GPU.

物理

Physics simulation is not thread-safe by default. To run the physics servers on separate threads (making them thread-safe), enable the following project settings:

GDScript arrays and dictionaries

In GDScript, reading and writing elements from multiple threads is OK, but anything that changes the container size (resizing, adding, or removing elements) requires locking a mutex.

資源

Modifying a unique resource from multiple threads is not supported. However, handling references on multiple threads is supported. Hence loading resources on a thread is as well - scenes, textures, meshes, etc - can be loaded and manipulated on a thread and then added to the active scene on the main thread. The limitation here is as described above: one must be careful not to load the same resource from multiple threads at once. Therefore, it's easiest to use one thread for loading and modifying resources, and then the main thread for adding them.