I have a bunch of independent tasks to process in separate threads. There is no data shared between threads, and each thread stashes its result in a dedicated variable. Once all threads are done, I would like to use the results to update the game state in a sequential manner, one by one.
Am I on the right track here?
extends Node
@onready var thread0: Thread = Thread.new()
@onready var thread1: Thread = Thread.new()
func _ready():
var result0 = thread0.start(Callable(self, "_do_something").bind(some_data))
var result1 = thread1.start(Callable(self, "_do_something_else").bind(more_data))
# Wait for all threads to complete. All threads should be done
# computing before proceeding any further.
while (thread0.is_alive() or thread1.is_alive()):
pass
# Do something with result0 and result1.
update_state_one(result0)
update_state_two(result1)
func _do_something(data):
# Process data.
call_deferred("_done_something", result)
func _do_something_else(data):
# Process data.
call_deferred("_done_something", result)
func _done_something(result):
thread.wait_to_finish()
return result