Thread-sichere APIs

Threads

Threads werden verwendet, um die Verarbeitungsleistung zwischen CPUs und Kernen zu verteilen. Godot unterstützt Multithreading, jedoch nicht in der gesamten Engine.

Nachfolgend finden Sie eine Liste der Möglichkeiten, wie Multithreading in verschiedenen Bereichen von Godot verwendet werden kann.

Globaler Bereich

Global Scope singletons are all thread-safe. Accessing servers from threads is supported (for VisualServer and Physics servers, ensure threaded or thread-safe operation is enabled in the project settings!).

Dies macht sie ideal für Code, der Zig-Tausende von Instanzen auf Servern erstellt und sie über Threads steuert. Natürlich erfordert es etwas mehr Code, da dieser direkt und nicht im Szenenbaum verwendet wird.

Szenen-Baum (SceneTree)

Die Interaktion mit dem aktiven Szenenbaum ist NICHT threadsicher. Stellen Sie sicher, dass Sie Mutexe verwenden, wenn Sie Daten zwischen Threads senden. Wenn Sie Funktionen von einem Thread aus aufrufen möchten, kann die Funktion call_deferred verwendet werden:

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

However, creating scene chunks (nodes in tree arrangement) outside the active tree is fine. This way, parts of a scene can be built or instantiated in a thread, then added in the main thread:

var enemy_scene = load("res://enemy_scene.scn")
var enemy = enemy_scene.instance()
enemy.add_child(weapon) # Set a weapon.
world.call_deferred("add_child", 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) tweaked by the multiple threads, resulting in unexpected behaviors or crashes.

Only use more than one thread to generate scene data if you really know what you are doing and you are sure that a single resource is not being used or set in multiple ones. Otherwise, you are safer just using the servers API (which is fully thread-safe) directly and not touching scene or resources.

Rendering

Instancing nodes that render anything in 2D or 3D (such as Sprite) is not thread-safe by default. To make rendering thread-safe, set the Rendering > Threads > Thread Model project setting to Multi-Threaded.

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

GDScript Arrays, 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.

Ressourcen

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 is easiest to use one thread for loading and modifying resources, and then the main thread for adding them.