Home Explore Blog CI



neovim

21th chunk of `runtime/doc/luaref.txt`
4ed2cd9539046b08d7383d549294b0290066da861ea8536d0000000100000fab
 references, then the garbage collector will collect this
object.

                                                                        *__mode*
A weak table can have weak keys, weak values, or both. A table with weak keys
allows the collection of its keys, but prevents the collection of its values.
A table with both weak keys and weak values allows the collection of both keys
and values. In any case, if either the key or the value is collected, the
whole pair is removed from the table. The weakness of a table is controlled by
the value of the `__mode` field of its metatable. If the `__mode` field is a
string containing the character `k`, the keys in the table are weak.
If `__mode` contains `v`, the values in the table are weak.

After you use a table as a metatable, you should not change the value of its
field `__mode`. Otherwise, the weak behavior of the tables controlled by this
metatable is undefined.

==============================================================================
2.11  Coroutines                                *lua-coroutine*

Lua supports coroutines, also called collaborative multithreading. A coroutine
in Lua represents an independent thread of execution. Unlike threads in
multithread systems, however, a coroutine only suspends its execution by
explicitly calling a yield function.

You create a coroutine with a call to `coroutine.create` (see
|coroutine.create()|). Its sole argument is a function that is the main
function of the coroutine. The `create` function only creates a new coroutine
and returns a handle to it (an object of type `thread`); it does not start the
coroutine execution.

When you first call `coroutine.resume` (see |coroutine.resume()|),
passing as its first argument the thread returned by `coroutine.create`, the
coroutine starts its execution, at the first line of its main function. Extra
arguments passed to `coroutine.resume` are passed on to the coroutine main
function. After the coroutine starts running, it runs until it terminates or
`yields`.

A coroutine can terminate its execution in two ways: normally, when its main
function returns (explicitly or implicitly, after the last instruction); and
abnormally, if there is an unprotected error. In the first case,
`coroutine.resume` returns `true`, plus any values returned by the coroutine
main function. In case of errors, `coroutine.resume` returns `false` plus an
error message.

A coroutine yields by calling `coroutine.yield` (see
|coroutine.yield()|). When a coroutine yields, the corresponding
`coroutine.resume` returns immediately, even if the yield happens inside
nested function calls (that is, not in the main function, but in a function
directly or indirectly called by the main function). In the case of a yield,
`coroutine.resume` also returns `true`, plus any values passed to
`coroutine.yield`. The next time you resume the same coroutine, it continues
its execution from the point where it yielded, with the call to
`coroutine.yield` returning any extra arguments passed to `coroutine.resume`.

Like `coroutine.create`, the `coroutine.wrap` function (see
|coroutine.wrap()|) also creates a coroutine, but instead of returning
the coroutine itself, it returns a function that, when called, resumes the
coroutine. Any arguments passed to this function go as extra arguments to
`coroutine.resume`. `coroutine.wrap` returns all the values returned by
`coroutine.resume`, except the first one (the boolean error code). Unlike
`coroutine.resume`, `coroutine.wrap` does not catch errors; any error is
propagated to the caller.

As an example, consider the next code:
>lua
       function foo1 (a)
         print("foo", a)
         return coroutine.yield(2*a)
       end

       co = coroutine.create(function (a,b)
             print("co-body", a, b)
             local r = foo1(a+1)
             print("co-body", r)
             local r, s = coroutine.yield(a+b, a-b)
             print("co-body", r, s)
             return b, "end"
       end)

       print("main",

Title: Lua Coroutines: Definition, Creation, Resuming, and Yielding
Summary
This section first details Lua's weak tables, explaining how they use weak references that are ignored by the garbage collector. The `__mode` field in the metatable controls whether keys and/or values are weak. It then introduces coroutines, a form of collaborative multithreading in Lua. It explains how to create coroutines with `coroutine.create`, resume them with `coroutine.resume`, and yield execution with `coroutine.yield`. It also discusses `coroutine.wrap`, which creates a function that resumes a coroutine, and provides an example to illustrate coroutine behavior.