Thread.xml 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Thread" inherits="Reference" 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]Note:[/b] Breakpoints won't break on code if it's running in a thread. This is a current limitation of the GDScript debugger.
  9. [b]Warning:[/b]
  10. 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:
  11. - It must not have any [Mutex] objects locked.
  12. - It must not be waiting on any [Semaphore] objects.
  13. - [method wait_to_finish] should have been called on it.
  14. </description>
  15. <tutorials>
  16. <link title="Using multiple threads">$DOCS_URL/tutorials/performance/threads/using_multiple_threads.html</link>
  17. <link title="Thread-safe APIs">$DOCS_URL/tutorials/performance/threads/thread_safe_apis.html</link>
  18. <link title="3D Voxel Demo">https://godotengine.org/asset-library/asset/676</link>
  19. </tutorials>
  20. <methods>
  21. <method name="get_id" qualifiers="const">
  22. <return type="String" />
  23. <description>
  24. Returns the current [Thread]'s ID, uniquely identifying it among all threads. If the [Thread] is not running this returns an empty string.
  25. </description>
  26. </method>
  27. <method name="is_active" qualifiers="const">
  28. <return type="bool" />
  29. <description>
  30. 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].
  31. </description>
  32. </method>
  33. <method name="is_alive" qualifiers="const">
  34. <return type="bool" />
  35. <description>
  36. Returns [code]true[/code] if this [Thread] is currently running. This is useful for determining if [method wait_to_finish] can be called without blocking the calling thread.
  37. To check if a [Thread] is joinable, use [method is_active].
  38. </description>
  39. </method>
  40. <method name="start">
  41. <return type="int" enum="Error" />
  42. <argument index="0" name="instance" type="Object" />
  43. <argument index="1" name="method" type="String" />
  44. <argument index="2" name="userdata" type="Variant" default="null" />
  45. <argument index="3" name="priority" type="int" enum="Thread.Priority" default="1" />
  46. <description>
  47. Starts a new [Thread] that runs [code]method[/code] on object [code]instance[/code] with [code]userdata[/code] passed as an argument. Even if no userdata is passed, [code]method[/code] must accept one argument and it will be null. The [code]priority[/code] of the [Thread] can be changed by passing a value from the [enum Priority] enum.
  48. Returns [constant OK] on success, or [constant ERR_CANT_CREATE] on failure.
  49. </description>
  50. </method>
  51. <method name="wait_to_finish">
  52. <return type="Variant" />
  53. <description>
  54. Joins the [Thread] and waits for it to finish. Returns the output of the method passed to [method start].
  55. 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].
  56. To determine if this can be called without blocking the calling thread, check if [method is_alive] is [code]false[/code].
  57. [b]Note:[/b] 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.
  58. </description>
  59. </method>
  60. </methods>
  61. <constants>
  62. <constant name="PRIORITY_LOW" value="0" enum="Priority">
  63. A thread running with lower priority than normally.
  64. </constant>
  65. <constant name="PRIORITY_NORMAL" value="1" enum="Priority">
  66. A thread with a standard priority.
  67. </constant>
  68. <constant name="PRIORITY_HIGH" value="2" enum="Priority">
  69. A thread running with higher priority than normally.
  70. </constant>
  71. </constants>
  72. </class>