# coroutine

This is an expansion to the existing Lua Coroutine library.

Coroutines are a very powerful way of splitting execution of a function up until some event occurs (for example, a timer fires, or input arrives).
The function chooses when to "yield" execution.

The yield / resume sequence allows variables to be passed back and forward between the thread and the caller.
For example the thread can yield with an argument which tells the caller why it yielded, and the caller can resume with an argument telling the thread why it was resumed.


# Functions

# coroutine.create(f: function): thread

coroutine.create(f: function): thread
  • Creates a thread consisting of the body f.

# coroutine.resume(t: thread, ...: any): boolean, ...any

coroutine.resume(t: thread, ...: any): boolean, ...any
  • Start or resume a thread created by coroutine.create. Any values supplied after the thread are returned as results from the coroutine.yield inside the thread.
  • If this is the first call for this thread, the values are supplied to the function itself.

# coroutine.running(): thread

coroutine.running(): thread
  • Returns the running coroutine, or no value when called by the main thread.

# coroutine.status(): string

coroutine.status(): string
  • Returns a string indicating the status of the thread.
  • Raises an error if the argument is not a thread.

# coroutine.wrap(f: function): function

coroutine.wrap(f: function): function
  • Creates a thread with body f, and then returns a function that can be used to resume the thread.

# coroutine.yield(...: any): ...any

coroutine.yield(...: any): ...any
  • Yields execution back to the caller, effectively creating co-operative multi-tasking, values supplied to yield are returned to coroutine.resume.
  • The coroutine cannot be running a C function, a metamethod, or an iterator.