Class WorkerThreadPoolInstance
- Namespace
- Godot
- Assembly
- GodotSharp.dll
The WorkerThreadPool singleton allocates a set of GodotThreads (called worker threads) on project startup and provides methods for offloading tasks to them. This can be used for simple multithreading without having to create GodotThreads.
Tasks hold the Callable to be run by the threads. WorkerThreadPool can be used to create regular tasks, which will be taken by one worker thread, or group tasks, which can be distributed between multiple worker threads. Group tasks execute the Callable multiple times, which makes them useful for iterating over a lot of elements, such as the enemies in an arena.
Here's a sample on how to offload an expensive function to worker threads:
private List<Node> _enemies = new List<Node>(); // A list to be filled with enemies.
private void ProcessEnemyAI(int enemyIndex)
{
Node processedEnemy = _enemies[enemyIndex];
// Expensive logic here.
}
public override void _Process(double delta)
{
long taskId = WorkerThreadPool.AddGroupTask(Callable.From<int>(ProcessEnemyAI), _enemies.Count);
// Other code...
WorkerThreadPool.WaitForGroupTaskCompletion(taskId);
// Other code that depends on the enemy AI already being processed.
}
The above code relies on the number of elements in the enemies
array remaining constant during the multithreaded part.
Note: Using this singleton could affect performance negatively if the task being distributed between threads is not computationally expensive.
[GodotClassName("WorkerThreadPool")]
public class WorkerThreadPoolInstance : GodotObject, IDisposable
- Inheritance
-
WorkerThreadPoolInstance
- Implements
- Inherited Members
Methods
AddGroupTask(Callable, int, int, bool, string)
Adds action
as a group task to be executed by the worker threads. The Callable will be called a number of times based on elements
, with the first thread calling it with the value 0
as a parameter, and each consecutive execution incrementing this value by 1 until it reaches element - 1
.
The number of threads the task is distributed to is defined by tasksNeeded
, where the default value -1
means it is distributed to all worker threads. highPriority
determines if the task has a high priority or a low priority (default). You can optionally provide a description
to help with debugging.
Returns a group task ID that can be used by other methods.
public long AddGroupTask(Callable action, int elements, int tasksNeeded = -1, bool highPriority = false, string description = "")
Parameters
Returns
AddTask(Callable, bool, string)
Adds action
as a task to be executed by a worker thread. highPriority
determines if the task has a high priority or a low priority (default). You can optionally provide a description
to help with debugging.
Returns a task ID that can be used by other methods.
public long AddTask(Callable action, bool highPriority = false, string description = "")
Parameters
Returns
GetGroupProcessedElementCount(long)
Returns how many times the Callable of the group task with the given ID has already been executed by the worker threads.
Note: If a thread has started executing the Callable but is yet to finish, it won't be counted.
public uint GetGroupProcessedElementCount(long groupId)
Parameters
groupId
long
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
IsGroupTaskCompleted(long)
Returns true
if the group task with the given ID is completed.
public bool IsGroupTaskCompleted(long groupId)
Parameters
groupId
long
Returns
IsTaskCompleted(long)
Returns true
if the task with the given ID is completed.
public bool IsTaskCompleted(long taskId)
Parameters
taskId
long
Returns
WaitForGroupTaskCompletion(long)
Pauses the thread that calls this method until the group task with the given ID is completed.
public void WaitForGroupTaskCompletion(long groupId)
Parameters
groupId
long
WaitForTaskCompletion(long)
Pauses the thread that calls this method until the task with the given ID is completed.
Returns Ok if the task could be successfully awaited.
Returns InvalidParameter if a task with the passed ID does not exist (maybe because it was already awaited and disposed of).
Returns Busy if the call is made from another running task and, due to task scheduling, the task to await is at a lower level in the call stack and therefore can't progress. This is an advanced situation that should only matter when some tasks depend on others.
public Error WaitForTaskCompletion(long taskId)
Parameters
taskId
long