Lua coroutine functions
Lua coroutine functions

These are the functions in the "coroutine" table.

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.

Personally I wouldn't use coroutine.wrap, but stick to something like this:


  • Create a new thread using coroutine.create. At this stage it is not yet running.
  • Commence executing the function in the thread with coroutine.resume, passing any initial arguments required.
  • The thread yields execution, if necessary, using coroutine.yield, passing arguments back to be returned by coroutine.resume. These arguments could indicate the reason for yielding.
  • The main script resumes the thread when it is ready to do so, calling coroutine.resume again. This time arguments passed to coroutine.resume are returned as results from the coroutine.yield call. These arguments could indicate the reason the thread is resuming (eg. data received, timeout and so on).
  • The previous 2 steps are repeated until it is time for the function to return, effectively terminating the thread.



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_bc Lua bc (big number) functions
DOC_lua_bit Lua bit manipulation functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_os Lua os functions
DOC_lua_package Lua package functions
DOC_lua_rex Lua PCRE regular expression functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions
DOC_lua_utils Lua utilities
DOC_scripting Scripting

Lua functions

LUA_coroutine.create coroutine.create (Creates a new coroutine thread)
LUA_coroutine.resume coroutine.resume (Start or resume a thread)
LUA_coroutine.running coroutine.running (Returns the running coroutine)
LUA_coroutine.status coroutine.status (Returns the status of a thread)
LUA_coroutine.wrap coroutine.wrap (Creates a thread and returns a function to resume it)
LUA_coroutine.yield coroutine.yield (Yields execution of thread back to the caller)

(Help topic: general=lua_coroutines)

DOC_contents Documentation contents page