| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="Thread" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | A unit of execution in a process. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | A unit of execution in a process. Can run methods on [Object]s simultaneously. The use of synchronization via [Mutex] or [Semaphore] is advised if working with shared objects. |
| 8 | [b]Warning:[/b] 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: |
| 9 | - It must not have any [Mutex] objects locked. |
| 10 | - It must not be waiting on any [Semaphore] objects. |
| 11 | - [method wait_to_finish] should have been called on it. |
| 12 | </description> |
| 13 | <tutorials> |
| 14 | <link title="Using multiple threads">$DOCS_URL/tutorials/performance/using_multiple_threads.html</link> |
| 15 | <link title="Thread-safe APIs">$DOCS_URL/tutorials/performance/thread_safe_apis.html</link> |
| 16 | <link title="3D Voxel Demo">https://godotengine.org/asset-library/asset/2755</link> |
| 17 | </tutorials> |
| 18 | <methods> |
| 19 | <method name="get_id" qualifiers="const"> |
| 20 | <return type="String" /> |
| 21 | <description> |
| 22 | Returns the current [Thread]'s ID, uniquely identifying it among all threads. If the [Thread] has not started running or if [method wait_to_finish] has been called, this returns an empty string. |
| 23 | </description> |
| 24 | </method> |
| 25 | <method name="is_alive" qualifiers="const"> |
| 26 | <return type="bool" /> |
| 27 | <description> |
| 28 | Returns [code]true[/code] if this [Thread] is currently running the provided function. This is useful for determining if [method wait_to_finish] can be called without blocking the calling thread. |
| 29 | To check if a [Thread] is joinable, use [method is_started]. |
| 30 | </description> |
| 31 | </method> |
| 32 | <method name="is_main_thread" qualifiers="static"> |
| 33 | <return type="bool" /> |
| 34 | <description> |
| 35 | Returns [code]true[/code] if the thread this method was called from is the main thread. |
| 36 | [b]Note:[/b] This is a static method and isn't associated with a specific [Thread] object. |
| 37 | </description> |
| 38 | </method> |
| 39 | <method name="is_started" qualifiers="const"> |
| 40 | <return type="bool" /> |
| 41 | <description> |
| 42 | Returns [code]true[/code] if this [Thread] has been started. Once started, this will return [code]true[/code] until it is joined using [method wait_to_finish]. For checking if a [Thread] is still executing its task, use [method is_alive]. |
| 43 | </description> |
| 44 | </method> |
| 45 | <method name="set_thread_safety_checks_enabled" qualifiers="static"> |
| 46 | <return type="void" /> |
| 47 | <param index="0" name="enabled" type="bool" /> |
| 48 | <description> |
| 49 | Sets whether the thread safety checks the engine normally performs in methods of certain classes (e.g., [Node]) should happen [b]on the current thread[/b]. |
| 50 | The default, for every thread, is that they are enabled (as if called with [param enabled] being [code]true[/code]). |
| 51 | 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. |
| 52 | Because of that, there may be cases where the user may want to disable them ([param enabled] being [code]false[/code]) to make certain operations allowed again. By doing so, it becomes the user's responsibility to ensure thread safety (e.g., by using [Mutex]) for those objects that are otherwise protected by the engine. |
| 53 | [b]Note:[/b] 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. |
| 54 | [b]Note:[/b] This is useful for scripts running on either arbitrary [Thread] objects or tasks submitted to the [WorkerThreadPool]. It doesn't apply to code running during [Node] group processing, where the checks will be always performed. |
| 55 | [b]Note:[/b] Even in the case of having disabled the checks in a [WorkerThreadPool] task, there's no need to re-enable them at the end. The engine will do so. |
| 56 | </description> |
| 57 | </method> |
| 58 | <method name="start"> |
| 59 | <return type="int" enum="Error" /> |
| 60 | <param index="0" name="callable" type="Callable" /> |
| 61 | <param index="1" name="priority" type="int" enum="Thread.Priority" default="1" /> |
| 62 | <description> |
| 63 | Starts a new [Thread] that calls [param callable]. |
| 64 | If the method takes some arguments, you can pass them using [method Callable.bind]. |
| 65 | The [param priority] of the [Thread] can be changed by passing a value from the [enum Priority] enum. |
| 66 | Returns [constant OK] on success, or [constant ERR_CANT_CREATE] on failure. |
| 67 | </description> |
| 68 | </method> |
| 69 | <method name="wait_to_finish"> |
| 70 | <return type="Variant" /> |
| 71 | <description> |
| 72 | Joins the [Thread] and waits for it to finish. Returns the output of the [Callable] passed to [method start]. |
| 73 | 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]. |
| 74 | To determine if this can be called without blocking the calling thread, check if [method is_alive] is [code]false[/code]. |
| 75 | </description> |
| 76 | </method> |
| 77 | </methods> |
| 78 | <constants> |
| 79 | <constant name="PRIORITY_LOW" value="0" enum="Priority"> |
| 80 | A thread running with lower priority than normally. |
| 81 | </constant> |
| 82 | <constant name="PRIORITY_NORMAL" value="1" enum="Priority"> |
| 83 | A thread with a standard priority. |
| 84 | </constant> |
| 85 | <constant name="PRIORITY_HIGH" value="2" enum="Priority"> |
| 86 | A thread running with higher priority than normally. |
| 87 | </constant> |
| 88 | </constants> |
| 89 | </class> |