WorkerThreadPool
A singleton that allocates some Thread
s on startup, used to offload tasks to these threads.
class WorkerThreadPool
The WorkerThreadPool
singleton allocates a set of Thread
s (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 Thread
s.
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:
The above code relies on the number of elements in the enemies
array remaining constant during the multithreaded part.
Superclasses
class Object
Base class for all other classes in the engine.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
static func addGroupTask(action: Callable, elements: Int32, tasksNeeded: Int32, highPriority: Bool, description: String
) -> Int Adds
action
as a group task to be executed by the worker threads. TheCallable
will be called a number of times based onelements
, with the first thread calling it with the value0
as a parameter, and each consecutive execution incrementing this value by 1 until it reacheselement - 1
.static func addTask(action: Callable, highPriority: Bool, description: String
) -> Int 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 adescription
to help with debugging.static func getGroupProcessedElementCount(groupId: Int
) -> UInt32 Returns how many times the
Callable
of the group task with the given ID has already been executed by the worker threads.static func isGroupTaskCompleted(groupId: Int
) -> Bool Returns
true
if the group task with the given ID is completed.static func isTaskCompleted(taskId: Int
) -> Bool Returns
true
if the task with the given ID is completed.static func waitForGroupTaskCompletion(groupId: Int
) Pauses the thread that calls this method until the group task with the given ID is completed.
static func waitForTaskCompletion(taskId: Int
) -> GodotError Pauses the thread that calls this method until the task with the given ID is completed.
class var godotClassName: StringName