Register forum user name Search FAQ

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.

Lua functions

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

Topics

Lua base functions
Lua bc (big number) functions
Lua bit manipulation functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua PCRE regular expression functions
Lua script extensions
Lua string functions
Lua syntax
Lua table functions
Lua utilities
Scripting
Scripting callbacks - plugins

(Help topic: general=lua_coroutines)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.