Skip to content

Commit 48330b0

Browse files
committed
Add a section for multithreading primitives to Core types.
1 parent fbec851 commit 48330b0

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

engine_details/architecture/core_types.rst

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,56 @@ scripting API.
150150
.. |typed_dictionary| replace:: `TypedDictionary <https://github.com/godotengine/godot/blob/master/core/variant/typed_dictionary.h>`__
151151
.. |pair| replace:: `Pair <https://github.com/godotengine/godot/blob/master/core/templates/pair.h>`__
152152

153-
Thread safety
154-
~~~~~~~~~~~~~
153+
.. _doc_core_concurrency_types:
154+
155+
Multithreading / Concurrency
156+
----------------------------
157+
158+
.. seealso::
159+
160+
You can find more information on multithreading strategies at :ref:`doc_using_multiple_threads`.
155161

156162
None of Godot's containers are thread-safe. When you expect multiple threads to access them, you must use multithread
157-
protections, like ``Mutex`` or ``Semaphore``.
163+
protections.
164+
165+
Note that some of the types listed here are also available through the bindings, but the binding types are wrapped with
166+
:ref:`class_RefCounted` (found in the ``CoreBind::`` namespace). Prefer the primitives listed here when possible, for
167+
efficiency reasons.
168+
169+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
170+
| Godot datatype | Closest C++ STL datatype | Comment |
171+
+=======================+==============================+=======================================================================================+
172+
| |mutex| | ``std::recursive_mutex`` | Recursive mutex type. Use ``MutexLock lock(mutex)`` to lock it. |
173+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
174+
| |binary_mutex| | ``std::mutex`` | Non-recursive mutex type. Use ``MutexLock lock(mutex)`` to lock it. |
175+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
176+
| |rw_lock| | ``std::shared_timed_mutex`` | Read-write aware mutex type. Use ``RWLockRead lock(mutex)`` or |
177+
| | | ``RWLockWrite lock(mutex)`` to lock it. |
178+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
179+
| |safe_binary_mutex| | | Recursive mutex type that can be used with ``ConditionVariable``. |
180+
| | | Use ``MutexLock lock(mutex)`` to lock it. |
181+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
182+
| |condition_variable| | ``std::condition_variable`` | Condition variable type, used with ``SafeBinaryMutex``. |
183+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
184+
| |semaphore| | ``std::counting_semaphore`` | Counting semaphore type. |
185+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
186+
| |safe_numeric| | ``std::atomic`` | Templated atomic type, designed for numbers. |
187+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
188+
| |safe_flag| | ``std::atomic_bool`` | Bool atomic type. |
189+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
190+
| |safe_ref_count| | ``std::atomic`` | Atomic type designed for reference counting. Will refuse to increment the |
191+
| | | reference count if it is 0. |
192+
+-----------------------+------------------------------+---------------------------------------------------------------------------------------+
193+
194+
.. |mutex| replace:: `Mutex <https://github.com/godotengine/godot/blob/master/core/os/mutex.h>`__
195+
.. |binary_mutex| replace:: `BinaryMutex <https://github.com/godotengine/godot/blob/master/core/os/mutex.h>`__
196+
.. |rw_lock| replace:: `RWLock <https://github.com/godotengine/godot/blob/master/core/os/rw_lock.h>`__
197+
.. |safe_binary_mutex| replace:: `SafeBinaryMutex <https://github.com/godotengine/godot/blob/master/core/os/safe_binary_mutex.h>`__
198+
.. |condition_variable| replace:: `ConditionVariable <https://github.com/godotengine/godot/blob/master/core/os/condition_variable.h>`__
199+
.. |semaphore| replace:: `Semaphore <https://github.com/godotengine/godot/blob/master/core/os/semaphore.h>`__
200+
.. |safe_numeric| replace:: `SafeNumeric <https://github.com/godotengine/godot/blob/master/core/templates/safe_refcount.h>`__
201+
.. |safe_flag| replace:: `SafeFlag <https://github.com/godotengine/godot/blob/master/core/templates/safe_refcount.h>`__
202+
.. |safe_ref_count| replace:: `SafeRefCount <https://github.com/godotengine/godot/blob/master/core/templates/safe_refcount.h>`__
158203

159204
Math types
160205
----------

engine_details/architecture/variant_class.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ An Array just holds an array of Variants. Of course, a Variant can also hold a
144144
Dictionary or an Array inside, making it even more flexible.
145145

146146
Modifications to a container will modify all references to
147-
it. A Mutex should be created to lock it if
147+
it. A :ref:`Mutex <doc_core_concurrency_types>` should be created to lock it if
148148
:ref:`multi-threaded access <doc_using_multiple_threads>` is desired.
149149

150150
References

tutorials/performance/using_multiple_threads.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Using multiple threads
44
======================
55

6+
.. seealso::
7+
8+
For a list of multithreading primitives in C++, see :ref:`doc_core_concurrency_types`.
9+
610
Threads
711
-------
812

0 commit comments

Comments
 (0)