Class GodotThread
- Namespace
- Godot
- Assembly
- GodotSharp.dll
A unit of execution in a process. Can run methods on GodotObjects simultaneously. The use of synchronization via Mutex or Semaphore is advised if working with shared objects.
Warning:
To ensure proper cleanup without crashes or deadlocks, when a GodotThread's reference count reaches zero and it is therefore destroyed, the following conditions must be met:
- It must not have any Mutex objects locked.
- It must not be waiting on any Semaphore objects.
- WaitToFinish() should have been called on it.
[GodotClassName("Thread")]
public class GodotThread : RefCounted, IDisposable
- Inheritance
-
GodotThread
- Implements
- Inherited Members
Constructors
GodotThread()
public GodotThread()
Methods
GetId()
Returns the current GodotThread's ID, uniquely identifying it among all threads. If the GodotThread has not started running or if WaitToFinish() has been called, this returns an empty string.
public string GetId()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsAlive()
Returns true
if this GodotThread is currently running the provided function. This is useful for determining if WaitToFinish() can be called without blocking the calling thread.
To check if a GodotThread is joinable, use IsStarted().
public bool IsAlive()
Returns
IsStarted()
Returns true
if this GodotThread has been started. Once started, this will return true
until it is joined using WaitToFinish(). For checking if a GodotThread is still executing its task, use IsAlive().
public bool IsStarted()
Returns
SetThreadSafetyChecksEnabled(bool)
Sets whether the thread safety checks the engine normally performs in methods of certain classes (e.g., 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 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 GodotThread 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.
Note: 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.
public static void SetThreadSafetyChecksEnabled(bool enabled)
Parameters
enabled
bool
Start(Callable, Priority)
Starts a new GodotThread that calls callable
.
If the method takes some arguments, you can pass them using Callable.bind
.
The priority
of the GodotThread can be changed by passing a value from the GodotThread.Priority enum.
Returns Ok on success, or CantCreate on failure.
public Error Start(Callable callable, GodotThread.Priority priority = Priority.Normal)
Parameters
callable
Callablepriority
GodotThread.Priority
Returns
WaitToFinish()
Joins the GodotThread and waits for it to finish. Returns the output of the Callable passed to Start(Callable, Priority).
Should either be used when you want to retrieve the value returned from the method called by the GodotThread or before freeing the instance that contains the GodotThread.
To determine if this can be called without blocking the calling thread, check if IsAlive() is false
.
public Variant WaitToFinish()