Skip to content

Commit 4bb75ac

Browse files
authored
Merge pull request #11431 from Calinou/update-thread-safe-apis
Update Thread-safe APIs for Godot 4.5
2 parents 99175ce + 7972487 commit 4bb75ac

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

tutorials/performance/thread_safe_apis.rst

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
:article_outdated: True
2-
31
.. _doc_thread_safe_apis:
42

53
Thread-safe APIs
@@ -16,14 +14,23 @@ Below is a list of ways multithreading can be used in different areas of Godot.
1614
Global scope
1715
------------
1816

19-
:ref:`Global Scope<class_@GlobalScope>` singletons are all thread-safe. Accessing servers from threads is supported (for RenderingServer and Physics servers, ensure threaded or thread-safe operation is enabled in the project settings!).
17+
Most :ref:`Global Scope<class_@GlobalScope>` singletons are thread-safe by default.
18+
Accessing servers from threads is supported. However, for the
19+
:ref:`rendering <doc_thread_safe_apis_rendering>` and
20+
:ref:`physics <doc_thread_safe_apis_physics>` servers,
21+
thread-safe operation must be enabled in the project settings first.
2022

21-
This makes them 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.
23+
This makes singletons ideal for code that creates dozens of thousands of instances
24+
in servers and controls them from threads. Of course, it requires a bit more
25+
code, as this is used directly and not within the scene tree.
2226

2327
Scene tree
2428
----------
2529

26-
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 from a thread, the *call_deferred* function may be used:
30+
Interacting with the active scene tree is **not** thread-safe. Make sure
31+
to use mutexes when sending data between threads. If you want to call
32+
functions or set properties from a thread, you may use
33+
:ref:`call_deferred <class_Object_method_call_deferred>` or :ref:`set_deferred <class_Object_method_set_deferred>`:
2734

2835
.. tabs::
2936
.. code-tab:: gdscript
@@ -40,7 +47,9 @@ Interacting with the active scene tree is **NOT** thread-safe. Make sure to use
4047
// Safe:
4148
node.CallDeferred(Node.MethodName.AddChild, childNode);
4249

43-
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:
50+
However, creating scene chunks (nodes in tree arrangement) outside the active
51+
tree is fine. This way, parts of a scene can be built or instantiated
52+
in a thread, then added in the main thread:
4453

4554
.. tabs::
4655
.. code-tab:: gdscript
@@ -58,36 +67,62 @@ However, creating scene chunks (nodes in tree arrangement) outside the active tr
5867
world.CallDeferred(Node.MethodName.AddChild, enemy);
5968

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

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

79+
.. _doc_thread_safe_apis_rendering:
80+
7081
Rendering
7182
---------
7283

73-
Instancing nodes that render anything in 2D or 3D (such as Sprite) is *not* thread-safe by default.
74-
To make rendering thread-safe, set the
75-
:ref:`Rendering > Driver > Thread Model<class_ProjectSettings_property_rendering/driver/threads/thread_model>`
76-
project setting to **Multi-Threaded**.
84+
Instancing nodes that render anything in 2D or 3D (such as :ref:`class_Sprite2D`
85+
or :ref:`class_MeshInstance3D`) is *not* thread-safe by default. To run the
86+
rendering driver on a separate thread, set the
87+
:ref:`Rendering > Driver > Thread Model <class_ProjectSettings_property_rendering/driver/threads/thread_model>`
88+
project setting to **Separate**.
7789

78-
Note that the Multi-Threaded thread model has several known bugs, so it may not be usable
90+
Note that the **Separate** thread model has several known bugs, so it may not be usable
7991
in all scenarios.
8092

81-
You should avoid calling functions involving direct interaction with the GPU on other threads, such as creating new textures
82-
or modifying and retrieving image data, these operations can lead to performance stalls because they require synchronization
83-
with the :ref:`RenderingServer<class_RenderingServer>`, as data needs to be transmitted to or updated on the GPU.
93+
.. warning::
94+
95+
You should avoid calling functions involving direct interaction with the GPU
96+
on other threads, such as creating new textures or modifying and retrieving
97+
image data. These operations can lead to performance stalls because they require
98+
synchronization with the :ref:`RenderingServer<class_RenderingServer>`,
99+
as data needs to be transmitted to or updated on the GPU.
100+
101+
.. _doc_thread_safe_apis_physics:
102+
103+
Physics
104+
-------
105+
106+
Physics simulation is *not* thread-safe by default. To run the physics servers
107+
on separate threads (making them thread-safe), enable the following project settings:
108+
109+
- **PhysicsServer2D:** :ref:`Physics > 2D > Run on Separate Thread <class_ProjectSettings_property_physics/2d/run_on_separate_thread>`.
110+
- **PhysicsServer3D:** :ref:`Physics > 3D > Run on Separate Thread <class_ProjectSettings_property_physics/3d/run_on_separate_thread>`.
84111

85-
GDScript arrays, dictionaries
86-
-----------------------------
112+
GDScript arrays and dictionaries
113+
--------------------------------
87114

88-
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.
115+
In GDScript, reading and writing elements from multiple threads is OK, but anything
116+
that changes the container size (resizing, adding, or removing elements) requires
117+
locking a :ref:`mutex <doc_using_multiple_threads_mutexes>`.
89118

90119
Resources
91120
---------
92121

93-
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.
122+
Modifying a unique resource from multiple threads is not supported. However,
123+
handling references on multiple threads *is* supported. Hence loading resources
124+
on a thread is as well - scenes, textures, meshes, etc - can be loaded and manipulated
125+
on a thread and then added to the active scene on the main thread. The limitation here
126+
is as described above: one must be careful not to load the same resource from
127+
multiple threads at once. Therefore, it's easiest to use **one** thread for loading
128+
and modifying resources, and then the main thread for adding them.

tutorials/performance/using_multiple_threads.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ wait until the thread is done (if not done yet), then properly dispose of it.
150150
operation. Locking should be done carefully; avoid locking too often (or for
151151
too long).
152152

153+
.. _doc_using_multiple_threads_mutexes:
154+
153155
Mutexes
154156
-------
155157

0 commit comments

Comments
 (0)