class_thread.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/Thread.xml.
  6. .. _class_Thread:
  7. Thread
  8. ======
  9. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A unit of execution in a process.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. A unit of execution in a process. Can run methods on :ref:`Object<class_Object>`\ s simultaneously. The use of synchronization via :ref:`Mutex<class_Mutex>` or :ref:`Semaphore<class_Semaphore>` is advised if working with shared objects.
  15. \ **Warning:**\
  16. To ensure proper cleanup without crashes or deadlocks, when a **Thread**'s reference count reaches zero and it is therefore destroyed, the following conditions must be met:
  17. - It must not have any :ref:`Mutex<class_Mutex>` objects locked.
  18. - It must not be waiting on any :ref:`Semaphore<class_Semaphore>` objects.
  19. - :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` should have been called on it.
  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. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/2755>`__
  26. .. rst-class:: classref-reftable-group
  27. Methods
  28. -------
  29. .. table::
  30. :widths: auto
  31. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | :ref:`String<class_String>` | :ref:`get_id<class_Thread_method_get_id>`\ (\ ) |const| |
  33. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`bool<class_bool>` | :ref:`is_alive<class_Thread_method_is_alive>`\ (\ ) |const| |
  35. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`bool<class_bool>` | :ref:`is_started<class_Thread_method_is_started>`\ (\ ) |const| |
  37. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | |void| | :ref:`set_thread_safety_checks_enabled<class_Thread_method_set_thread_safety_checks_enabled>`\ (\ enabled\: :ref:`bool<class_bool>`\ ) |static| |
  39. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_Thread_method_start>`\ (\ callable\: :ref:`Callable<class_Callable>`, priority\: :ref:`Priority<enum_Thread_Priority>` = 1\ ) |
  41. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Variant<class_Variant>` | :ref:`wait_to_finish<class_Thread_method_wait_to_finish>`\ (\ ) |
  43. +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  44. .. rst-class:: classref-section-separator
  45. ----
  46. .. rst-class:: classref-descriptions-group
  47. Enumerations
  48. ------------
  49. .. _enum_Thread_Priority:
  50. .. rst-class:: classref-enumeration
  51. enum **Priority**: :ref:`🔗<enum_Thread_Priority>`
  52. .. _class_Thread_constant_PRIORITY_LOW:
  53. .. rst-class:: classref-enumeration-constant
  54. :ref:`Priority<enum_Thread_Priority>` **PRIORITY_LOW** = ``0``
  55. A thread running with lower priority than normally.
  56. .. _class_Thread_constant_PRIORITY_NORMAL:
  57. .. rst-class:: classref-enumeration-constant
  58. :ref:`Priority<enum_Thread_Priority>` **PRIORITY_NORMAL** = ``1``
  59. A thread with a standard priority.
  60. .. _class_Thread_constant_PRIORITY_HIGH:
  61. .. rst-class:: classref-enumeration-constant
  62. :ref:`Priority<enum_Thread_Priority>` **PRIORITY_HIGH** = ``2``
  63. A thread running with higher priority than normally.
  64. .. rst-class:: classref-section-separator
  65. ----
  66. .. rst-class:: classref-descriptions-group
  67. Method Descriptions
  68. -------------------
  69. .. _class_Thread_method_get_id:
  70. .. rst-class:: classref-method
  71. :ref:`String<class_String>` **get_id**\ (\ ) |const| :ref:`🔗<class_Thread_method_get_id>`
  72. Returns the current **Thread**'s ID, uniquely identifying it among all threads. If the **Thread** has not started running or if :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` has been called, this returns an empty string.
  73. .. rst-class:: classref-item-separator
  74. ----
  75. .. _class_Thread_method_is_alive:
  76. .. rst-class:: classref-method
  77. :ref:`bool<class_bool>` **is_alive**\ (\ ) |const| :ref:`🔗<class_Thread_method_is_alive>`
  78. Returns ``true`` if this **Thread** is currently running the provided function. This is useful for determining if :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` can be called without blocking the calling thread.
  79. To check if a **Thread** is joinable, use :ref:`is_started<class_Thread_method_is_started>`.
  80. .. rst-class:: classref-item-separator
  81. ----
  82. .. _class_Thread_method_is_started:
  83. .. rst-class:: classref-method
  84. :ref:`bool<class_bool>` **is_started**\ (\ ) |const| :ref:`🔗<class_Thread_method_is_started>`
  85. Returns ``true`` if this **Thread** has been started. Once started, this will return ``true`` until it is joined using :ref:`wait_to_finish<class_Thread_method_wait_to_finish>`. For checking if a **Thread** is still executing its task, use :ref:`is_alive<class_Thread_method_is_alive>`.
  86. .. rst-class:: classref-item-separator
  87. ----
  88. .. _class_Thread_method_set_thread_safety_checks_enabled:
  89. .. rst-class:: classref-method
  90. |void| **set_thread_safety_checks_enabled**\ (\ enabled\: :ref:`bool<class_bool>`\ ) |static| :ref:`🔗<class_Thread_method_set_thread_safety_checks_enabled>`
  91. Sets whether the thread safety checks the engine normally performs in methods of certain classes (e.g., :ref:`Node<class_Node>`) should happen **on the current thread**.
  92. The default, for every thread, is that they are enabled (as if called with ``enabled`` being ``true``).
  93. Those checks are conservative. That means that they will only succeed in considering a call thread-safe (and therefore allow it to happen) if the engine can guarantee such safety.
  94. Because of that, there may be cases where the user may want to disable them (``enabled`` being ``false``) to make certain operations allowed again. By doing so, it becomes the user's responsibility to ensure thread safety (e.g., by using :ref:`Mutex<class_Mutex>`) for those objects that are otherwise protected by the engine.
  95. \ **Note:** This is an advanced usage of the engine. You are advised to use it only if you know what you are doing and there is no safer way.
  96. \ **Note:** This is useful for scripts running on either arbitrary **Thread** objects or tasks submitted to the :ref:`WorkerThreadPool<class_WorkerThreadPool>`. It doesn't apply to code running during :ref:`Node<class_Node>` group processing, where the checks will be always performed.
  97. \ **Note:** Even in the case of having disabled the checks in a :ref:`WorkerThreadPool<class_WorkerThreadPool>` task, there's no need to re-enable them at the end. The engine will do so.
  98. .. rst-class:: classref-item-separator
  99. ----
  100. .. _class_Thread_method_start:
  101. .. rst-class:: classref-method
  102. :ref:`Error<enum_@GlobalScope_Error>` **start**\ (\ callable\: :ref:`Callable<class_Callable>`, priority\: :ref:`Priority<enum_Thread_Priority>` = 1\ ) :ref:`🔗<class_Thread_method_start>`
  103. Starts a new **Thread** that calls ``callable``.
  104. If the method takes some arguments, you can pass them using :ref:`Callable.bind<class_Callable_method_bind>`.
  105. The ``priority`` of the **Thread** can be changed by passing a value from the :ref:`Priority<enum_Thread_Priority>` enum.
  106. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or :ref:`@GlobalScope.ERR_CANT_CREATE<class_@GlobalScope_constant_ERR_CANT_CREATE>` on failure.
  107. .. rst-class:: classref-item-separator
  108. ----
  109. .. _class_Thread_method_wait_to_finish:
  110. .. rst-class:: classref-method
  111. :ref:`Variant<class_Variant>` **wait_to_finish**\ (\ ) :ref:`🔗<class_Thread_method_wait_to_finish>`
  112. Joins the **Thread** and waits for it to finish. Returns the output of the :ref:`Callable<class_Callable>` passed to :ref:`start<class_Thread_method_start>`.
  113. Should either be used when you want to retrieve the value returned from the method called by the **Thread** or before freeing the instance that contains the **Thread**.
  114. To determine if this can be called without blocking the calling thread, check if :ref:`is_alive<class_Thread_method_is_alive>` is ``false``.
  115. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  116. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  117. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  118. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  119. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  120. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  121. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  122. .. |void| replace:: :abbr:`void (No return value.)`