Coroutines
Coroutines allow for the distribution and execution of multiple tasks as a set of concurrent processes. Coroutines do not provide parallelism: they can not be used to distribute work across multiple cores.
Syntax
Coroutines can be created in two different ways, either by defining a function and passing it to coroutine.create()
, or declaring the relevant logic as an anonymous function within the body of the coroutine.create()
statement.
-- Option 1
local newFunc = function()
-- Function body
end
end
local routineA = coroutine.create(newFunc)
-- Option 2
local routineB = coroutine.create(
function() -- Anonymous function declaration
-- Function body
if -- Condition x
coroutine.yield() -- The coroutine is paused until it is called again
end
end
)
Coroutine States
A coroutine can enter one of three states: suspended, running, or dead. This is what makes them useful, as they are essentially functions that can be paused and resumed at a later point. The status of a coroutine can be checked with the status()
function and resumed using the resume()
function. Once a coroutine has reached a dead state, it cannot be resumed again.
Functions in Coroutines
The following functions can be used to create, manage and terminate coroutines.
All contributors
- Anonymous contributorAnonymous contributor2 total contributions
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.