You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/performance/thread_safe_apis.rst
+56-21Lines changed: 56 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,3 @@
1
-
:article_outdated: True
2
-
3
1
.. _doc_thread_safe_apis:
4
2
5
3
Thread-safe APIs
@@ -16,14 +14,23 @@ Below is a list of ways multithreading can be used in different areas of Godot.
16
14
Global scope
17
15
------------
18
16
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
thread-safe operation must be enabled in the project settings first.
20
22
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.
22
26
23
27
Scene tree
24
28
----------
25
29
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>`:
27
34
28
35
.. tabs::
29
36
.. code-tab:: gdscript
@@ -40,7 +47,9 @@ Interacting with the active scene tree is **NOT** thread-safe. Make sure to use
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:
44
53
45
54
.. tabs::
46
55
.. code-tab:: gdscript
@@ -58,36 +67,62 @@ However, creating scene chunks (nodes in tree arrangement) outside the active tr
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**.
77
89
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
79
91
in all scenarios.
80
92
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>`.
84
111
85
-
GDScript arrays, dictionaries
86
-
-----------------------------
112
+
GDScript arrays and dictionaries
113
+
--------------------------------
87
114
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>`.
89
118
90
119
Resources
91
120
---------
92
121
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.
0 commit comments