thread

Contents

thread#

A thread is a coroutine handle, the value returned by coroutine.create. Coroutines are cooperative, not OS threads; control transfers only when the running coroutine explicitly yields.

Create a coroutine; it does not start running until resume.

local co = coroutine.create(function(a)
  coroutine.yield(a + 1)
  coroutine.yield(a + 2)
end)

First resume starts the coroutine and runs until the first yield.

print(coroutine.resume(co, 10))  --> true  11

Subsequent resume calls pick up where the coroutine yielded.

print(coroutine.resume(co))      --> true  12

See Concurrency for the cooperative model in full.

References#

  • Concurrency for cooperative scheduling and the full coroutine surface.

  • function for the first-class function coroutine.create wraps.