:github_url: hide .. _class_Semaphore: Semaphore ========= **Hérite de :** :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 Tutoriels ------------------ - :doc:`Utiliser plusieurs fils d'exécution <../tutorials/performance/using_multiple_threads>` - :doc:`Les API sûres pour plusieurs fils d'exécution <../tutorials/performance/thread_safe_apis>` .. rst-class:: classref-reftable-group Méthodes ---------------- .. 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 Descriptions des méthodes -------------------------------------------------- .. _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 (Cette méthode doit typiquement être redéfinie par l'utilisateur pour avoir un effet.)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (Cette méthode n'a pas d'effets de bord. Elle ne modifie aucune des variables membres de l'instance.)` .. |vararg| replace:: :abbr:`vararg (Cette méthode accepte n'importe quel nombre d'arguments après ceux décris ici.)` .. |constructor| replace:: :abbr:`constructor (Cette méthode est utilisée pour construire un type.)` .. |static| replace:: :abbr:`static (Cette méthode n'a pas besoin d'instance pour être appelée, elle peut donc être directement appelée en utilisant le nom de la classe.)` .. |operator| replace:: :abbr:`operator (Cette méthode décrit un opérateur valide à utiliser avec ce type en tant qu'opérande gauche.)` .. |bitfield| replace:: :abbr:`BitField (Cette valeur est un nombre entier composé d'un masque de bits des options suivantes.)` .. |void| replace:: :abbr:`void (Aucune valeur de retour.)`