Wait for infinite loop?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Wait for infinite loop?

Post 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.
Last edited by kikito on Wed Mar 21, 2012 9:27 am, edited 1 time in total.
When I write def I mean function.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Wait for infinite loop?

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Wait for infinite loop?

Post by bartbes »

kikito wrote:corroutines
They're coroutines, not corroutines, thought you should know.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Wait for infinite loop?

Post by kikito »

bartbes wrote:
kikito wrote:corroutines
They're coroutines, not corroutines, thought you should know.
Yes. Fixing my post.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests