Currently we can only run threads without arguments. If we want to pass in data we need to use global variables:
global foo_bar = 0
thread foo_thread():
local bar = foo_bar
textmsg('fooing with bar: ', bar)
end
def foo(bar):
global foo_bar = bar
local id = run foo_thread()
return id # I'm not sure if you can 'return run foo_thread()' directly.
end
foo(5)
This works to a fashion, but:
- it pollutes the global namespace.
- it is prone to programmer error mixing up local/global variable names.
- it is susceptible to race conditions if you want to run the same code multiple times in different threads.
For example, this wont work as expected with the above implementation:
foo(4)
foo(5)
# Will print "fooing with bar: 5" twice.
It would be awesome if we could just do this:
thread foo(bar):
textmsg('fooing with bar: ', bar)
end
run foo(5)
# Now this should work fine too:
run foo(4)
run foo(5)