LuaCoroutines


Syntax

  • coroutine.create(function) returns a coroutine (type(coroutine) == 'thread') containing the function.

  • coroutine.resume(co, ...) resume, or start the coroutine. Any additional arguments given to resume are returned from the coroutine.yield() that previously paused the coroutine. If the coroutine had not been started the additional arguments become the arguments of the function.

  • coroutine.yield(...) yields the currently running coroutine. Execution picks back up after the call to coroutine.resume() that started that coroutine. Any arguments given to yield are returned from the corresponding coroutine.resume() that started the coroutine.

  • coroutine.status(co) returns the status of the coroutine, which can be :

    • "dead" : the function in the coroutine has reached it's end and the coroutine cannot be resumed anymore
    • "running" : the coroutine has been resumed and is running
    • "normal" : the coroutine has resumed another coroutine
    • "suspended" : the coroutine has yielded, and is waiting to be resumed
  • coroutine.wrap(function) returns a function that when called resumes the coroutine that would have been created by coroutine.create(function).

Remarks

The coroutine system has been implemented in lua to emulate multithreading existing in other languages. It works by switching at extremely high speed between different functions so that the human user think they are executed at the same time.