Re: Wait for infinite loop?
Posted: Tue Mar 20, 2012 8:21 am
I think mentioning threads when talking about coroutines makes them seem more confusing than they are.
The way I think about coroutines is this: they are functions that you can "interrupt" to do something else, and then "go back" to them. When you go back to a corroutine, it conserves its state - local variables and the like. There are specific orders to "interrupt" and "continue" them. Very usually, coroutines look like functions with a loop inside them, with the "interrupt" and/or "continue" orders somewhere in the loop body.
You can't execute coroutines in parallel, as you would do with threads. But they can "ping-pong": corroutine A does its thing, and then "yields" to corroutine B. When B finishes doing its thing, it "gives power back" to A, and A continues (maybe yielding to corroutine C). B can be later on be "awakened" again, and it will continue work where it left it, until it yields again (or finishes).
Coroutines put the programmer "in charge" of the "switching" between tasks. With threads, everything runs in parallel, so sometimes you have to put code to do the inverse: force the threads to "block and wait".
The disadvantage of coroutines is that blocking calls blocks everything - for example, you can't "load resources in a corroutine while you do stuff in another". The first corroutine will just "stall", and not "give control" to the second.
Their main advantage is that they are provided in all Lua implementations, while threads are platform-specific (that's why we have love.thread). This happens because Lua is implemented using C99, which doesn't specify native threading.
The way I think about coroutines is this: they are functions that you can "interrupt" to do something else, and then "go back" to them. When you go back to a corroutine, it conserves its state - local variables and the like. There are specific orders to "interrupt" and "continue" them. Very usually, coroutines look like functions with a loop inside them, with the "interrupt" and/or "continue" orders somewhere in the loop body.
You can't execute coroutines in parallel, as you would do with threads. But they can "ping-pong": corroutine A does its thing, and then "yields" to corroutine B. When B finishes doing its thing, it "gives power back" to A, and A continues (maybe yielding to corroutine C). B can be later on be "awakened" again, and it will continue work where it left it, until it yields again (or finishes).
Coroutines put the programmer "in charge" of the "switching" between tasks. With threads, everything runs in parallel, so sometimes you have to put code to do the inverse: force the threads to "block and wait".
The disadvantage of coroutines is that blocking calls blocks everything - for example, you can't "load resources in a corroutine while you do stuff in another". The first corroutine will just "stall", and not "give control" to the second.
Their main advantage is that they are provided in all Lua implementations, while threads are platform-specific (that's why we have love.thread). This happens because Lua is implemented using C99, which doesn't specify native threading.