> 2. Does NOT say that the thread must be created by a call to lua_newthread(). Yes it does. > To start a coroutine, you first create a new thread (see lua_newthread); ... On Sat, Dec 13, 2014 at 4:43 PM, Sergei Zhirikov < dmarc-noreply@xxxxxxxxxxxxx> wrote: > > > > >> Wait, I misread the docs, again. > > misread how? AFAICT, the docs about lua_resume() state that it > > starts/resumes a coroutine, nothing else. the given code puts a > > function that calls coroutine.yield(), but it never builds a coroutine > > (either by coroutine.create(f) or lua_newthread()) > > > If we are going to refer to the docs, let's read them precisely. > The docs say that lua_resume() "Starts and resumes a coroutine in a given > thread." Which: > 1. Clearly indicates that a coroutine is not the same as a thread, but > rather something that runs in a thread. > > 2. Does NOT say that the thread must be created by a call to > lua_newthread(). > > So, the code in question start a coroutine (represented by the loaded > function) in the main Lua thread. You don't "build" a couroutine, because > there is nothing to build. You need a thread and a function and a call to > lua_resume() - that's all there is to it. If you feel that a coroutine is > something more than that - that's just your imagination, because it isn't. > > The Lua API to manipulate coroutines may give one a different impression, > as it is slightly higher level than the C API. When using Lua API you have > to create a new thread, which coroutine.create() does internally, for that > simple reason that it is the only thing that makes sense, because the > existing thread is already occupied running the very code that is trying to > start a new coroutine - this has nothing to do with the nature of the > coroutine as such. When using the C API, such a restriction does not exist. > > -- Sincerely, Alex Parrill