Universal Robots Forum

URScript: passing arguments to threads

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)
3 Likes

Note: as a bonus it would be cool if we can run any function in a new thread without requiring to define it with the thread keyword:

def foo(bar):
     textmsg("fooing with bar: ", bar)
end

foo(5)     # Will execute foo(5) in this thread
run foo(5) # Will execute foo(5) in a new thread
1 Like