class_mutex.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/Mutex.xml.
  6. .. _class_Mutex:
  7. Mutex
  8. =====
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. A synchronization mutex (mutual exclusion).
  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 ever acquire the lock at a time. A mutex can be used to protect a critical section; however, be careful to avoid deadlocks.
  15. It's of the recursive kind, so it can be locked multiple times by one thread, provided it also unlocks it as many times.
  16. \ **Warning:**\
  17. To guarantee that the operating system is able to perform proper cleanup (no crashes, no deadlocks), these conditions must be met:
  18. - By the time a **Mutex**'s reference count reaches zero and therefore it is destroyed, no threads (including the one on which the destruction will happen) must have it locked.
  19. - By the time a :ref:`Thread<class_Thread>`'s reference count reaches zero and therefore it is destroyed, it must not have any mutex locked.
  20. .. rst-class:: classref-introduction-group
  21. Tutorials
  22. ---------
  23. - :doc:`../tutorials/performance/threads/using_multiple_threads`
  24. .. rst-class:: classref-reftable-group
  25. Methods
  26. -------
  27. .. table::
  28. :widths: auto
  29. +---------------------------------------+----------------------------------------------------------+
  30. | void | :ref:`lock<class_Mutex_method_lock>` **(** **)** |
  31. +---------------------------------------+----------------------------------------------------------+
  32. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`try_lock<class_Mutex_method_try_lock>` **(** **)** |
  33. +---------------------------------------+----------------------------------------------------------+
  34. | void | :ref:`unlock<class_Mutex_method_unlock>` **(** **)** |
  35. +---------------------------------------+----------------------------------------------------------+
  36. .. rst-class:: classref-section-separator
  37. ----
  38. .. rst-class:: classref-descriptions-group
  39. Method Descriptions
  40. -------------------
  41. .. _class_Mutex_method_lock:
  42. .. rst-class:: classref-method
  43. void **lock** **(** **)**
  44. Locks this **Mutex**, blocks until it is unlocked by the current owner.
  45. \ **Note:** This function returns without blocking if the thread already has ownership of the mutex.
  46. .. rst-class:: classref-item-separator
  47. ----
  48. .. _class_Mutex_method_try_lock:
  49. .. rst-class:: classref-method
  50. :ref:`Error<enum_@GlobalScope_Error>` **try_lock** **(** **)**
  51. Tries locking this **Mutex**, but does not block. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, :ref:`@GlobalScope.ERR_BUSY<class_@GlobalScope_constant_ERR_BUSY>` otherwise.
  52. \ **Note:** This function returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` if the thread already has ownership of the mutex.
  53. .. rst-class:: classref-item-separator
  54. ----
  55. .. _class_Mutex_method_unlock:
  56. .. rst-class:: classref-method
  57. void **unlock** **(** **)**
  58. Unlocks this **Mutex**, leaving it to other threads.
  59. \ **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.
  60. \ **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.
  61. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  62. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  63. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  64. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`