Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
Passing parameters to a coroutine in Lua
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
4755 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).