class_thread.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/Thread.xml.
  6. .. _class_Thread:
  7. Thread
  8. ======
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :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. \ **Note:** Breakpoints won't break on code if it's running in a thread. This is a current limitation of the GDScript debugger.
  16. \ **Warning:**\
  17. To guarantee that the operating system is able to perform proper cleanup (no crashes, no deadlocks), these conditions must be met by the time a **Thread**'s reference count reaches zero and therefore it is destroyed:
  18. - It must not have any :ref:`Mutex<class_Mutex>` objects locked.
  19. - It must not be waiting on any :ref:`Semaphore<class_Semaphore>` objects.
  20. - :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` should have been called on it.
  21. .. rst-class:: classref-introduction-group
  22. Tutorials
  23. ---------
  24. - :doc:`Using multiple threads <../tutorials/performance/threads/using_multiple_threads>`
  25. - :doc:`Thread-safe APIs <../tutorials/performance/threads/thread_safe_apis>`
  26. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  27. .. rst-class:: classref-reftable-group
  28. Methods
  29. -------
  30. .. table::
  31. :widths: auto
  32. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`String<class_String>` | :ref:`get_id<class_Thread_method_get_id>` **(** **)** |const| |
  34. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`bool<class_bool>` | :ref:`is_active<class_Thread_method_is_active>` **(** **)** |const| |
  36. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`bool<class_bool>` | :ref:`is_alive<class_Thread_method_is_alive>` **(** **)** |const| |
  38. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`start<class_Thread_method_start>` **(** :ref:`Object<class_Object>` instance, :ref:`String<class_String>` method, :ref:`Variant<class_Variant>` userdata=null, :ref:`Priority<enum_Thread_Priority>` priority=1 **)** |
  40. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Variant<class_Variant>` | :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` **(** **)** |
  42. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. .. rst-class:: classref-section-separator
  44. ----
  45. .. rst-class:: classref-descriptions-group
  46. Enumerations
  47. ------------
  48. .. _enum_Thread_Priority:
  49. .. rst-class:: classref-enumeration
  50. enum **Priority**:
  51. .. _class_Thread_constant_PRIORITY_LOW:
  52. .. rst-class:: classref-enumeration-constant
  53. :ref:`Priority<enum_Thread_Priority>` **PRIORITY_LOW** = ``0``
  54. A thread running with lower priority than normally.
  55. .. _class_Thread_constant_PRIORITY_NORMAL:
  56. .. rst-class:: classref-enumeration-constant
  57. :ref:`Priority<enum_Thread_Priority>` **PRIORITY_NORMAL** = ``1``
  58. A thread with a standard priority.
  59. .. _class_Thread_constant_PRIORITY_HIGH:
  60. .. rst-class:: classref-enumeration-constant
  61. :ref:`Priority<enum_Thread_Priority>` **PRIORITY_HIGH** = ``2``
  62. A thread running with higher priority than normally.
  63. .. rst-class:: classref-section-separator
  64. ----
  65. .. rst-class:: classref-descriptions-group
  66. Method Descriptions
  67. -------------------
  68. .. _class_Thread_method_get_id:
  69. .. rst-class:: classref-method
  70. :ref:`String<class_String>` **get_id** **(** **)** |const|
  71. Returns the current **Thread**'s ID, uniquely identifying it among all threads. If the **Thread** is not running this returns an empty string.
  72. .. rst-class:: classref-item-separator
  73. ----
  74. .. _class_Thread_method_is_active:
  75. .. rst-class:: classref-method
  76. :ref:`bool<class_bool>` **is_active** **(** **)** |const|
  77. 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>`.
  78. .. rst-class:: classref-item-separator
  79. ----
  80. .. _class_Thread_method_is_alive:
  81. .. rst-class:: classref-method
  82. :ref:`bool<class_bool>` **is_alive** **(** **)** |const|
  83. Returns ``true`` if this **Thread** is currently running. This is useful for determining if :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` can be called without blocking the calling thread.
  84. To check if a **Thread** is joinable, use :ref:`is_active<class_Thread_method_is_active>`.
  85. .. rst-class:: classref-item-separator
  86. ----
  87. .. _class_Thread_method_start:
  88. .. rst-class:: classref-method
  89. :ref:`Error<enum_@GlobalScope_Error>` **start** **(** :ref:`Object<class_Object>` instance, :ref:`String<class_String>` method, :ref:`Variant<class_Variant>` userdata=null, :ref:`Priority<enum_Thread_Priority>` priority=1 **)**
  90. Starts a new **Thread** that runs ``method`` on object ``instance`` with ``userdata`` passed as an argument. Even if no userdata is passed, ``method`` must accept one argument and it will be null. The ``priority`` of the **Thread** can be changed by passing a value from the :ref:`Priority<enum_Thread_Priority>` enum.
  91. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or :ref:`@GlobalScope.ERR_CANT_CREATE<class_@GlobalScope_constant_ERR_CANT_CREATE>` on failure.
  92. .. rst-class:: classref-item-separator
  93. ----
  94. .. _class_Thread_method_wait_to_finish:
  95. .. rst-class:: classref-method
  96. :ref:`Variant<class_Variant>` **wait_to_finish** **(** **)**
  97. Joins the **Thread** and waits for it to finish. Returns the output of the method passed to :ref:`start<class_Thread_method_start>`.
  98. 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**.
  99. To determine if this can be called without blocking the calling thread, check if :ref:`is_alive<class_Thread_method_is_alive>` is ``false``.
  100. \ **Note:** After the **Thread** finishes joining it will be disposed. If you want to use it again you will have to create a new instance of it.
  101. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  102. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  103. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  104. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`