resumeThread(t: thread, args: ...any)
The resumeThread (name pending) function behaves similar to coroutine.resume
: it receives a thread and a number of arguments, and resumes the thread, passing the arguments to the thread. The difference is that no values are returned, and errors are handled by the engine, in the same way as spawn
, delay
, events, and so on. I believe that the addition of this function is an essential piece to properly integrating coroutines with the Roblox engine.
Developers generally have several requirements in regards to threads:
- Immediate: There must be no significant delay between resuming the thread and the thread actually running.
- Error handling: Errors must be handled by the engine. The engine has several error-monitoring mechanisms that require errors to be properly handled in order to be usable (LogService.MessageOut, ScriptContext.Error, dev console).
- Inexpensive: Spawning threads shouldn’t require big chunky objects.
- Arguments: Passing arguments to the thread should be supported.
Let’s enumerate the various ways of spawning threads, and how they meet these requirements:
coroutine.create/resume
- Immediate: thread runs immediately with no delay.
- No error handling: errors must be handled manually, with no means to pass the error to the engine.
- Inexpensive: the baseline for threads in terms of performance.
- Supports arguments: resume receives arguments to be passed to the thread and returns values returned by the thread.
coroutine.wrap
- Immediate: the function runs immediately with no delay.
- Poor error handling: errors that occur within the thread are emitted directly, killing the caller of the wrapper.
- Inexpensive: just a function that wraps coroutine.create/resume.
- Supports arguments: wrap receives arguments to be passed to the thread and returns values returned by the thread.
spawn
- Not immediate: the created thread is resumed after a minimum wait time.
- Proper error handling: errors are handled by the engine.
- Inexpensive: just a function.
- No argument support: spawn cannot receive arguments. A closure must be used.
delay
- Not immediate: the created thread is resumed after a specified delay, but this delay has a minimum wait time.
- Proper error handling: errors are handled by the engine.
- Inexpensive: just a function.
- No argument support: delay cannot receive arguments. A closure must be used.
events
- Immediate: listeners run immediately with no delay.
- Proper error handling: errors are handled by the engine.
- Expensive: requires creating an instance with extra baggage that has nothing to do with threads.
- Poor argument support: BindableEvents can pass arguments to listeners, but the values are copied.
Finally, these are compared to the resumeThread function:
resumeThread
- Immediate: thread runs immediately with no delay.
- Correct error handling: errors are handled by the engine.
- Inexpensive: just a function.
- Supports arguments: resumeThread receives arguments to be passed to the thread.