: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` **<** :ref:`Object` A synchronization mechanism used to control access to a shared resource by :ref:`Thread`\ s. .. rst-class:: classref-introduction-group Description ----------- A synchronization semaphore that can be used to synchronize multiple :ref:`Thread`\ s. Initialized to zero on creation. For a binary version, see :ref:`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`'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`\ (\ count\: :ref:`int` = 1\ ) | +-------------------------+-----------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`try_wait`\ (\ ) | +-------------------------+-----------------------------------------------------------------------------------+ | |void| | :ref:`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` = 1\ ) :ref:`🔗` 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` **try_wait**\ (\ ) :ref:`🔗` Like :ref:`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:`🔗` 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.)` .. |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.)`