class_thread.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Thread.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Thread:
  6. Thread
  7. ======
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. A unit of execution in a process.
  10. Description
  11. -----------
  12. 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.
  13. **Note:** Breakpoints won't break on code if it's running in a thread. This is a current limitation of the GDScript debugger.
  14. Tutorials
  15. ---------
  16. - :doc:`../tutorials/performance/threads/using_multiple_threads`
  17. - :doc:`../tutorials/performance/threads/thread_safe_apis`
  18. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  19. Methods
  20. -------
  21. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`String<class_String>` | :ref:`get_id<class_Thread_method_get_id>` **(** **)** |const| |
  23. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`bool<class_bool>` | :ref:`is_active<class_Thread_method_is_active>` **(** **)** |const| |
  25. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`bool<class_bool>` | :ref:`is_alive<class_Thread_method_is_alive>` **(** **)** |const| |
  27. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :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 **)** |
  29. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :ref:`Variant<class_Variant>` | :ref:`wait_to_finish<class_Thread_method_wait_to_finish>` **(** **)** |
  31. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. Enumerations
  33. ------------
  34. .. _enum_Thread_Priority:
  35. .. _class_Thread_constant_PRIORITY_LOW:
  36. .. _class_Thread_constant_PRIORITY_NORMAL:
  37. .. _class_Thread_constant_PRIORITY_HIGH:
  38. enum **Priority**:
  39. - **PRIORITY_LOW** = **0** --- A thread running with lower priority than normally.
  40. - **PRIORITY_NORMAL** = **1** --- A thread with a standard priority.
  41. - **PRIORITY_HIGH** = **2** --- A thread running with higher priority than normally.
  42. Method Descriptions
  43. -------------------
  44. .. _class_Thread_method_get_id:
  45. - :ref:`String<class_String>` **get_id** **(** **)** |const|
  46. Returns the current ``Thread``'s ID, uniquely identifying it among all threads. If the ``Thread`` is not running this returns an empty string.
  47. ----
  48. .. _class_Thread_method_is_active:
  49. - :ref:`bool<class_bool>` **is_active** **(** **)** |const|
  50. 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>`.
  51. ----
  52. .. _class_Thread_method_is_alive:
  53. - :ref:`bool<class_bool>` **is_alive** **(** **)** |const|
  54. 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.
  55. To check if a ``Thread`` is joinable, use :ref:`is_active<class_Thread_method_is_active>`.
  56. ----
  57. .. _class_Thread_method_start:
  58. - :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 **)**
  59. 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.
  60. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or :ref:`@GlobalScope.ERR_CANT_CREATE<class_@GlobalScope_constant_ERR_CANT_CREATE>` on failure.
  61. ----
  62. .. _class_Thread_method_wait_to_finish:
  63. - :ref:`Variant<class_Variant>` **wait_to_finish** **(** **)**
  64. Joins the ``Thread`` and waits for it to finish. Returns the output of the method passed to :ref:`start<class_Thread_method_start>`.
  65. 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``.
  66. To determine if this can be called without blocking the calling thread, check if :ref:`is_alive<class_Thread_method_is_alive>` is ``false``.
  67. **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.
  68. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  69. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  70. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`