Skip to content

coroutine

The coroutine module is a native module of lua. For use, see: lua official manual

coroutine.create

  • Create a new coroutine

Function Prototype

API

lua
coroutine.create(f: <function>)

Parameter Description

ParameterDescription
fFunction to create coroutine

Usage

Creates a new coroutine with function f. Returns a thread object that represents the coroutine.

lua
local co = coroutine.create(function()
    print("Hello from coroutine")
end)

coroutine.resume

  • Resume execution of a coroutine

Function Prototype

API

lua
coroutine.resume(co: <thread>, ...)

Parameter Description

ParameterDescription
coCoroutine thread
...Arguments to pass to the coroutine

Usage

Starts or continues the execution of coroutine co. Returns true if the coroutine execution is successful, false otherwise.

lua
local success, result = coroutine.resume(co, "arg1", "arg2")

coroutine.yield

  • Yield execution to the caller

Function Prototype

API

lua
coroutine.yield(...)

Parameter Description

ParameterDescription
...Values to return to the caller

Usage

Suspends the execution of the calling coroutine and returns values to the caller.

lua
coroutine.yield("value1", "value2")

coroutine.status

  • Get the status of a coroutine

Function Prototype

API

lua
coroutine.status(co: <thread>)

Parameter Description

ParameterDescription
coCoroutine thread

Usage

Returns the status of coroutine co: "suspended", "running", "normal", or "dead".

lua
local status = coroutine.status(co)

coroutine.wrap

  • Create a coroutine wrapper function

Function Prototype

API

lua
coroutine.wrap(f: <function>)

Parameter Description

ParameterDescription
fFunction to create coroutine

Usage

Creates a new coroutine with function f and returns a function that resumes the coroutine each time it is called.

lua
local func = coroutine.wrap(function()
    return "Hello"
end)
local result = func()

coroutine.running

  • Get the running coroutine

Function Prototype

API

lua
coroutine.running()

Parameter Description

No parameters required for this function.

Usage

Returns the running coroutine, or nil when called by the main thread.

lua
local co = coroutine.running()