Files
godot-docs/classes/class_semaphore.rst
2025-06-07 03:33:02 +00:00

107 lines
4.3 KiB
ReStructuredText

:github_url: hide
.. DO NOT EDIT THIS FILE!!!
.. Generated automatically from Godot engine sources.
.. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
.. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Semaphore.xml.
.. _class_Semaphore:
Semaphore
=========
**Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
A synchronization mechanism used to control access to a shared resource by :ref:`Thread<class_Thread>`\ s.
.. rst-class:: classref-introduction-group
Description
-----------
A synchronization semaphore that can be used to synchronize multiple :ref:`Thread<class_Thread>`\ s. Initialized to zero on creation. For a binary version, see :ref:`Mutex<class_Mutex>`.
\ **Warning:** Semaphores must be used carefully to avoid deadlocks.
\ **Warning:** To guarantee that the operating system is able to perform proper cleanup (no crashes, no deadlocks), these conditions must be met:
- When a **Semaphore**'s reference count reaches zero and it is therefore destroyed, no threads must be waiting on it.
- When a :ref:`Thread<class_Thread>`'s reference count reaches zero and it is therefore destroyed, it must not be waiting on any semaphore.
.. rst-class:: classref-introduction-group
Tutorials
---------
- :doc:`Using multiple threads <../tutorials/performance/using_multiple_threads>`
- :doc:`Thread-safe APIs <../tutorials/performance/thread_safe_apis>`
.. rst-class:: classref-reftable-group
Methods
-------
.. table::
:widths: auto
+-------------------------+-----------------------------------------------------------------------------------+
| |void| | :ref:`post<class_Semaphore_method_post>`\ (\ count\: :ref:`int<class_int>` = 1\ ) |
+-------------------------+-----------------------------------------------------------------------------------+
| :ref:`bool<class_bool>` | :ref:`try_wait<class_Semaphore_method_try_wait>`\ (\ ) |
+-------------------------+-----------------------------------------------------------------------------------+
| |void| | :ref:`wait<class_Semaphore_method_wait>`\ (\ ) |
+-------------------------+-----------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Method Descriptions
-------------------
.. _class_Semaphore_method_post:
.. rst-class:: classref-method
|void| **post**\ (\ count\: :ref:`int<class_int>` = 1\ ) :ref:`🔗<class_Semaphore_method_post>`
Lowers the **Semaphore**, allowing one thread in, or more if ``count`` is specified.
.. rst-class:: classref-item-separator
----
.. _class_Semaphore_method_try_wait:
.. rst-class:: classref-method
:ref:`bool<class_bool>` **try_wait**\ (\ ) :ref:`🔗<class_Semaphore_method_try_wait>`
Like :ref:`wait()<class_Semaphore_method_wait>`, but won't block, so if the value is zero, fails immediately and returns ``false``. If non-zero, it returns ``true`` to report success.
.. rst-class:: classref-item-separator
----
.. _class_Semaphore_method_wait:
.. rst-class:: classref-method
|void| **wait**\ (\ ) :ref:`🔗<class_Semaphore_method_wait>`
Waits for the **Semaphore**, if its value is zero, blocks until non-zero.
.. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
.. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
.. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
.. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
.. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
.. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
.. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
.. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
.. |void| replace:: :abbr:`void (No return value.)`