class_mutex.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Mutex.xml.
  6. .. _class_Mutex:
  7. Mutex
  8. =====
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A binary :ref:`Semaphore<class_Semaphore>` for synchronization of multiple :ref:`Thread<class_Thread>`\ s.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. A synchronization mutex (mutual exclusion). This is used to synchronize multiple :ref:`Thread<class_Thread>`\ s, and is equivalent to a binary :ref:`Semaphore<class_Semaphore>`. It guarantees that only one thread can access a critical section at a time.
  15. This is a reentrant mutex, meaning that it can be locked multiple times by one thread, provided it also unlocks it as many times.
  16. \ **Warning:** Mutexes must be used carefully to avoid deadlocks.
  17. \ **Warning:** To ensure proper cleanup without crashes or deadlocks, the following conditions must be met:
  18. - When a **Mutex**'s reference count reaches zero and it is therefore destroyed, no threads (including the one on which the destruction will happen) must have it locked.
  19. - When a :ref:`Thread<class_Thread>`'s reference count reaches zero and it is therefore destroyed, it must not have any mutex locked.
  20. .. rst-class:: classref-introduction-group
  21. Tutorials
  22. ---------
  23. - :doc:`Using multiple threads <../tutorials/performance/using_multiple_threads>`
  24. - :doc:`Thread-safe APIs <../tutorials/performance/thread_safe_apis>`
  25. .. rst-class:: classref-reftable-group
  26. Methods
  27. -------
  28. .. table::
  29. :widths: auto
  30. +-------------------------+----------------------------------------------------------+
  31. | void | :ref:`lock<class_Mutex_method_lock>` **(** **)** |
  32. +-------------------------+----------------------------------------------------------+
  33. | :ref:`bool<class_bool>` | :ref:`try_lock<class_Mutex_method_try_lock>` **(** **)** |
  34. +-------------------------+----------------------------------------------------------+
  35. | void | :ref:`unlock<class_Mutex_method_unlock>` **(** **)** |
  36. +-------------------------+----------------------------------------------------------+
  37. .. rst-class:: classref-section-separator
  38. ----
  39. .. rst-class:: classref-descriptions-group
  40. Method Descriptions
  41. -------------------
  42. .. _class_Mutex_method_lock:
  43. .. rst-class:: classref-method
  44. void **lock** **(** **)**
  45. Locks this **Mutex**, blocks until it is unlocked by the current owner.
  46. \ **Note:** This function returns without blocking if the thread already has ownership of the mutex.
  47. .. rst-class:: classref-item-separator
  48. ----
  49. .. _class_Mutex_method_try_lock:
  50. .. rst-class:: classref-method
  51. :ref:`bool<class_bool>` **try_lock** **(** **)**
  52. Tries locking this **Mutex**, but does not block. Returns ``true`` on success, ``false`` otherwise.
  53. \ **Note:** This function returns ``true`` if the thread already has ownership of the mutex.
  54. .. rst-class:: classref-item-separator
  55. ----
  56. .. _class_Mutex_method_unlock:
  57. .. rst-class:: classref-method
  58. void **unlock** **(** **)**
  59. Unlocks this **Mutex**, leaving it to other threads.
  60. \ **Note:** If a thread called :ref:`lock<class_Mutex_method_lock>` or :ref:`try_lock<class_Mutex_method_try_lock>` multiple times while already having ownership of the mutex, it must also call :ref:`unlock<class_Mutex_method_unlock>` the same number of times in order to unlock it correctly.
  61. \ **Warning:** Calling :ref:`unlock<class_Mutex_method_unlock>` more times that :ref:`lock<class_Mutex_method_lock>` on a given thread, thus ending up trying to unlock a non-locked mutex, is wrong and may causes crashes or deadlocks.
  62. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  63. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  64. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  65. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  66. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  67. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  68. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`