:github_url: hide .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/4.0/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/4.0/doc/classes/Mutex.xml. .. _class_Mutex: Mutex ===== **Inherits:** :ref:`RefCounted` **<** :ref:`Object` A binary :ref:`Semaphore` for synchronization of multiple :ref:`Thread`\ s. .. rst-class:: classref-introduction-group Description ----------- A synchronization mutex (mutual exclusion). This is used to synchronize multiple :ref:`Thread`\ s, and is equivalent to a binary :ref:`Semaphore`. It guarantees that only one thread can ever acquire the lock at a time. A mutex can be used to protect a critical section; however, be careful to avoid deadlocks. .. 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:`lock` **(** **)** | +-------------------------+----------------------------------------------------------+ | :ref:`bool` | :ref:`try_lock` **(** **)** | +-------------------------+----------------------------------------------------------+ | void | :ref:`unlock` **(** **)** | +-------------------------+----------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Method Descriptions ------------------- .. _class_Mutex_method_lock: .. rst-class:: classref-method void **lock** **(** **)** Locks this **Mutex**, blocks until it is unlocked by the current owner. \ **Note:** This function returns without blocking if the thread already has ownership of the mutex. .. rst-class:: classref-item-separator ---- .. _class_Mutex_method_try_lock: .. rst-class:: classref-method :ref:`bool` **try_lock** **(** **)** Tries locking this **Mutex**, but does not block. Returns ``true`` on success, ``false`` otherwise. \ **Note:** This function returns ``true`` if the thread already has ownership of the mutex. .. rst-class:: classref-item-separator ---- .. _class_Mutex_method_unlock: .. rst-class:: classref-method void **unlock** **(** **)** Unlocks this **Mutex**, leaving it to other threads. \ **Note:** If a thread called :ref:`lock` or :ref:`try_lock` multiple times while already having ownership of the mutex, it must also call :ref:`unlock` the same number of times in order to unlock it correctly. .. |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.)`