I don't know if it's the best way, but you can pass the value from function to function.
In order to have threads working you need 3 functions: a main function that starts the thread; a function that is the thread; and a function that takes the result of the thread and processes it.
func main():
# Creates and calls the thread
var thr = Thread.new()
thr.start(self, "algorithm", thr)
func algorithm(thr):
# Take the thread reference as an argument
# Do the heavy processing
var product = [heavy algorithm]
call_deferred("post_processing", thr)
return product
func post_processing(thr):
# Receives the thread reference as an argument
var product = thr.wait_to_finish()
[do stuff with product]
With this you can always keep the reference of the thread and, when you want more than one thread, you can build an array (almost a queue) with these references and then, in the post_processing method, decide how to deal with the output of each individual thread, but use the same code for all the threads you create.
Again, I don't know if this is the best way to do threads, I've learn about them today, but I have this working for me.