class-description NEWS COMMUNITY STORE TUTORIALS SIGN UP LOGIN LOGOUT ROKOJORI NEWSLETTER SIGN UP LOGIN LOGOUT NEWS COMMUNITY STORE TUTORIALS TOGGLE FULLSCREEN VOLLBILD AN/AUS Object WorkerThreadPool
A singleton that allocates some Threads on startup, used to offload tasks to these threads.

The WorkerThreadPool singleton allocates a set of Threads (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 Threads.

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:

var enemies = [] # An array to be filled with enemies. func process_enemy_ai(enemy_index): var processed_enemy = enemies[enemy_index] # Expensive logic... func _process(delta): var task_id = WorkerThreadPool.add_group_task(process_enemy_ai, enemies.size()) # Other code... WorkerThreadPool.wait_for_group_task_completion(task_id) # 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.

int add_group_task<>( Callable action=, action:Callable=, int elements=, elements:int=, int=, int:=, bool high_priority=false, high_priority:bool=false, String description="", description:String="", ):int

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 tasks_needed, where the default value -1 means it is distributed to all worker threads. high_priority 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.

int add_task<>( Callable action=, action:Callable=, bool high_priority=false, high_priority:bool=false, String description="", description:String="", ):int

Adds action as a task to be executed by a worker thread. high_priority 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.

int get_group_processed_element_count<>( int group_id=, group_id:int=, ):int

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.

bool is_group_task_completed<>( int group_id=, group_id:int=, ):bool

Returns true if the group task with the given ID is completed.

bool is_task_completed<>( int task_id=, task_id:int=, ):bool

Returns true if the task with the given ID is completed.

void wait_for_group_task_completion<>( int group_id=, group_id:int=, ):void

Pauses the thread that calls this method until the group task with the given ID is completed.

Error wait_for_task_completion<>( int task_id=, task_id:int=, ):Error

Pauses the thread that calls this method until the task with the given ID is completed.

Returns @GlobalScope.OK if the task could be successfully awaited.

Returns @GlobalScope.ERR_INVALID_PARAMETER if a task with the passed ID does not exist (maybe because it was already awaited and disposed of).

Returns @GlobalScope.ERR_BUSY if the call is made from another running task and, due to task scheduling, there's potential for deadlocking (e.g., the task to await may be 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 (in the current implementation, the tricky case is a task trying to wait on an older one).




All social media brands are registrated trademarks and belong to their respective owners.





CONTACT IMPRINT TERMS OF USE PRIVACY © ROKOROJI ® 2021 rokojori.com
CONTACT IMPRINT TERMS OF USE PRIVACY © ROKOROJI ® 2021 rokojori.com
We are using cookies on this site. Read more... Wir benutzen Cookies auf dieser Seite. Mehr lesen...