:github_url: hide .. DO NOT EDIT THIS FILE!!! .. Generated automatically from Godot engine sources. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Thread.xml. .. _class_Thread: Thread ====== **Inherits:** :ref:`RefCounted` **<** :ref:`Object` A unit of execution in a process. .. rst-class:: classref-introduction-group Description ----------- A unit of execution in a process. Can run methods on :ref:`Object`\ s simultaneously. The use of synchronization via :ref:`Mutex` or :ref:`Semaphore` is advised if working with shared objects. \ **Warning:**\ 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: - It must not have any :ref:`Mutex` objects locked. - It must not be waiting on any :ref:`Semaphore` objects. - :ref:`wait_to_finish` should have been called on it. .. rst-class:: classref-introduction-group Tutorials --------- - :doc:`Using multiple threads <../tutorials/performance/using_multiple_threads>` - :doc:`Thread-safe APIs <../tutorials/performance/thread_safe_apis>` - `3D Voxel Demo `__ .. rst-class:: classref-reftable-group Methods ------- .. table:: :widths: auto +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_id`\ (\ ) |const| | +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_alive`\ (\ ) |const| | +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`is_started`\ (\ ) |const| | +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_thread_safety_checks_enabled`\ (\ enabled\: :ref:`bool`\ ) |static| | +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Error` | :ref:`start`\ (\ callable\: :ref:`Callable`, priority\: :ref:`Priority` = 1\ ) | +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`wait_to_finish`\ (\ ) | +---------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Enumerations ------------ .. _enum_Thread_Priority: .. rst-class:: classref-enumeration enum **Priority**: :ref:`🔗` .. _class_Thread_constant_PRIORITY_LOW: .. rst-class:: classref-enumeration-constant :ref:`Priority` **PRIORITY_LOW** = ``0`` A thread running with lower priority than normally. .. _class_Thread_constant_PRIORITY_NORMAL: .. rst-class:: classref-enumeration-constant :ref:`Priority` **PRIORITY_NORMAL** = ``1`` A thread with a standard priority. .. _class_Thread_constant_PRIORITY_HIGH: .. rst-class:: classref-enumeration-constant :ref:`Priority` **PRIORITY_HIGH** = ``2`` A thread running with higher priority than normally. .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group Method Descriptions ------------------- .. _class_Thread_method_get_id: .. rst-class:: classref-method :ref:`String` **get_id**\ (\ ) |const| :ref:`🔗` 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` has been called, this returns an empty string. .. rst-class:: classref-item-separator ---- .. _class_Thread_method_is_alive: .. rst-class:: classref-method :ref:`bool` **is_alive**\ (\ ) |const| :ref:`🔗` Returns ``true`` if this **Thread** is currently running the provided function. This is useful for determining if :ref:`wait_to_finish` can be called without blocking the calling thread. To check if a **Thread** is joinable, use :ref:`is_started`. .. rst-class:: classref-item-separator ---- .. _class_Thread_method_is_started: .. rst-class:: classref-method :ref:`bool` **is_started**\ (\ ) |const| :ref:`🔗` Returns ``true`` if this **Thread** has been started. Once started, this will return ``true`` until it is joined using :ref:`wait_to_finish`. For checking if a **Thread** is still executing its task, use :ref:`is_alive`. .. rst-class:: classref-item-separator ---- .. _class_Thread_method_set_thread_safety_checks_enabled: .. rst-class:: classref-method |void| **set_thread_safety_checks_enabled**\ (\ enabled\: :ref:`bool`\ ) |static| :ref:`🔗` Sets whether the thread safety checks the engine normally performs in methods of certain classes (e.g., :ref:`Node`) should happen **on the current thread**. The default, for every thread, is that they are enabled (as if called with ``enabled`` being ``true``). 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. 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`) for those objects that are otherwise protected by the engine. \ **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. \ **Note:** This is useful for scripts running on either arbitrary **Thread** objects or tasks submitted to the :ref:`WorkerThreadPool`. It doesn't apply to code running during :ref:`Node` group processing, where the checks will be always performed. \ **Note:** Even in the case of having disabled the checks in a :ref:`WorkerThreadPool` task, there's no need to re-enable them at the end. The engine will do so. .. rst-class:: classref-item-separator ---- .. _class_Thread_method_start: .. rst-class:: classref-method :ref:`Error` **start**\ (\ callable\: :ref:`Callable`, priority\: :ref:`Priority` = 1\ ) :ref:`🔗` Starts a new **Thread** that calls ``callable``. If the method takes some arguments, you can pass them using :ref:`Callable.bind`. The ``priority`` of the **Thread** can be changed by passing a value from the :ref:`Priority` enum. Returns :ref:`@GlobalScope.OK` on success, or :ref:`@GlobalScope.ERR_CANT_CREATE` on failure. .. rst-class:: classref-item-separator ---- .. _class_Thread_method_wait_to_finish: .. rst-class:: classref-method :ref:`Variant` **wait_to_finish**\ (\ ) :ref:`🔗` Joins the **Thread** and waits for it to finish. Returns the output of the :ref:`Callable` passed to :ref:`start`. 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**. To determine if this can be called without blocking the calling thread, check if :ref:`is_alive` is ``false``. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)` .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)` .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)` .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)` .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)` .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)` .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)` .. |void| replace:: :abbr:`void (No return value.)`