Page 2 of 2

Re: Wait for infinite loop?

Posted: Tue Mar 20, 2012 8:21 am
by kikito
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.

Re: Wait for infinite loop?

Posted: Tue Mar 20, 2012 3:20 pm
by Inny
Yeah, that is a better way of describing it. Multitasking is probably the better term than Multithreading. Although, Lua itself does complicate things by calling the object returned by coroutine.create a thread. coroutine.wrap is easier to work with, as that behaves much more like python's generators.

A great way to use coroutines in LOVE is to implement a kind of sprite/actor system. For instance, here's a snippet from something I'm working on (sprite just tests A* by moving between points on a map):

Code: Select all

function Erhardt:run()
  while true do
    self:wait( 3 )
    local target = string.format("ERHARDT%i", math.random(1, 3))
    local x, y = self.map:locateEntity( EntityCode[target] )
    if x then
      local p = PathFinder.getPath( self, x, y, self.map )
      self:move( p )
    else
      print("Couldn't find", target)
    end
  end
end
We're way off topic now. :D

Re: Wait for infinite loop?

Posted: Tue Mar 20, 2012 6:39 pm
by bartbes
kikito wrote:corroutines
They're coroutines, not corroutines, thought you should know.

Re: Wait for infinite loop?

Posted: Wed Mar 21, 2012 9:26 am
by kikito
bartbes wrote:
kikito wrote:corroutines
They're coroutines, not corroutines, thought you should know.
Yes. Fixing my post.