lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Le 21 juin 2016 à 16:24, Andrew Starks <andrew@starksfam.org> a écrit :
> 
> My question is not about how to use Lua; it's about how to explain it.
> 
> I'm writing a tutorial on coroutines. I'm explaining what a coroutine
> is, but feel that I need to explain the difference between it and the
> value of type `thread` that is returned by coroutine.create. As a
> newbie, I would expect a type of `coroutine` to be returned and since
> it isn't, I assume that there is a distinction to be made.
> 
> Is a coroutine the function that you pass into `coroutine.create`?
> 
> Is a `thread` the execution environment that is running the coroutine?
> 
>   thread = coroutine + stack + current state

I would say that a coroutine is simply a specific use case of a Lua thread: 

- you create it with `coroutine.create`
- you run it with `coroutine.resume`
- it can return control to the caller thread for any point of its execution using `coroutine.yield`.

Actually, the coroutine is the thread, not the function passed to `coroutine.create`.

Jean-Luc