Lua's coroutines, like Python's generators, provide for something which is akin to lightweight threading - rather than a strict functional calling stack, where one function must be completed before another is invoked, you can call a code block (function or method) and have it yield back to the calling routine - i.e. suspend operation - without running to completion.
In Lua ... it works like this:
• You create a coroutine with coroutine.create
which you pass a function, and it returns a variable (of type thread) which you use as a parameter to ...
; called the first time, this starts the coroutine running until it hits ...
which suspends operation and returns control to the resume.
• You enclose your coroutine.resume
in a loop, checking for a false return to indicate it has completed.
The question was asked on Thursday's course (and an excellent question
) "How do I pass parameters to and from a coroutine, and looking at my stock answers, I realise that the question had hit a slightly embarrassing hole in my resources. Because it isn't obvious, and you WILL want to pass values in and out of the coroutine, considering how closely the co-existent routines are, logically, to each other.
The answer is as follows
1. You CANNOT pass parameters in to the coroutine function in coroutine.create
. Thinking about it, it would be illogical to do so at the early stage of defining the routine; you're defining and nit running it after all, so you won't necessarily have your inputs ready anyway!
2. You can pass the parameters to start the co-routine in as the second (and subsequent) parameters to your first coroutine.resume
3. You can pass further data into the coroutine as the second and subsequent parameters to your subsequent coroutine.resume
calls, and they will be returned by the coroutine.yield
within the coroutine function
4. Data is passed back from the co-routine at each coroutine.yield
... parameters to the yield become the second and subsequent return values from coroutine.resume
Sample program ... source code here
... sample results:
[trainee@easterton toweb]$ lua inco
hello 1 gerald
dd 11 nil
hello 2 gerald
dd 11 nil
hello 3 gerald
dd 11 nil
hello 10 gerald
dd 11 nil
hello 11 gerald
dd 11 nil
hello nil nil
As well as a single co-routine implementation, where it is useful as a data factory, or as a processing member (as in my example code), you can have whole tables of coroutines; they become very useful in simulations, where each coroutine call advances the simulation / makes the next game move for a different character of other game element. (This is just one of the reasons that Lua is making it so big in the games industry at the moment ... and you can simulate not only game movements, but things like town growth too ...) (written 2009-08-01)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesU114 - Lua - Threading and Coroutines. 
Embedding Lua to perform tailored code at an interval - (2014-05-03) 
Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17) 
Lua examples - coroutines, error handling, objects, etc - (2009-10-15) 
What to do with a huge crop of apples - (2008-11-04) 
If you are learning Lua, here are some more examples - (2008-07-06) 
Co-routines in Lua - co-operative processing - (2008-06-29)
Some other Articles
For Lua Programmers AND for Town PlannersHow to make a Risotto (PHP build style)Guadalajara - a special tour of a lovely cityNew Lua Examples - for last weeks delegatesPassing parameters to a coroutine in LuaSeeing Guadalajara - a first glimpse downtownWhat are closures in Lua?Floor to ceilingLearning to write high quality code in LuaLua training class in Spanish