Page 1 of 1

Thread to thread communication in Löve 0.9.0 - Discussion

Posted: Sat Feb 01, 2014 1:57 pm
by Germanunkol
Hi,

I've been doing quite some work with threads in 0.8.0 and was familiar with that interface. The new interface has some improvements, but there's also a few features I'm missing and I'd like to hear/discuss some best practices and solutions to a few problems I have.

So, general question: How do YOU handle thread communication in 0.9.x?

Follow up questions (Note: I often use threads to render an image or similar, so I need input arguments - image size, width, height etc - and output arguments - the image. I also like to have some way of controlling the thread while it's running and a percentage update which tells me how far the thread has gotten):
1) When I start mutliple threads, all doing the same thing (and using the same file), how do you make sure channels are created and associated with the correct thread? For example, inside the thread, I use:
chanPercentageOut = love.thread.getChannel("percentage")
But then they'd all use the same value
2) Did anyone come up with a nice way of passing (key, value) pairs through channels? The 0.8.0 method had the nice feature to give a name (or key) to the value by which it could be retreived (with the disadvantage of it overwriting previous values with the same key). This is no longer possible. Still, sometimes, I like to mimik that behaviour, since otherwise I have a huge amount of channels that I need to create, if I have many kinds of values passed (of the same type, that is. Otherwise I could just identify them by the type).
One way I can come up with is to write a library that creates a channel automatically when it gets a new type of value (or asks for one). But this all seems very cumbersome...

Any discussion on thread-to-thread communication is welcome!

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sat Feb 01, 2014 2:11 pm
by Robin
For 1: I have no idea.

As for 2:

I just create a slew of named channels for each key/value pair I want to send. That may not be the best way of doing things.

Anyway, if you want to send multiple values at once, you can send a flat table that contains the key/value pairs you want to send.

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sat Feb 01, 2014 2:18 pm
by Germanunkol
Robin wrote: Anyway, if you want to send multiple values at once, you can send a flat table that contains the key/value pairs you want to send.
Hum, I don't need multiple things at the same time, but this is still an idea: I can simply use:

Code: Select all

channelOut:push( {key="percentage", value=10} )
.... somewhat ugly, but works...

This doesn't replace a

Code: Select all

thread:demand("percentage")
though.

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sat Feb 01, 2014 2:23 pm
by bartbes
Channels aren't ever associated to any thread, which is kind of the idea behind them.
From what I can tell, what you want to do is talk to a thread "in private", in this case, use an unnamed channel, and pass it to [wiki]Thread:start[/wiki] for instance.

Regarding key/value pairs, this is trivially re-implemented using channel:push{[key] = value}. (And then another trivial unpacking on the other end.)

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sat Feb 01, 2014 3:16 pm
by Robin
bartbes wrote:pass it to [wiki]Thread:start[/wiki] for instance.
Oh, man, I didn't realise this was possible! That solves all kinds of problems.

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sat Feb 01, 2014 8:36 pm
by Germanunkol
Robin wrote:
bartbes wrote:pass it to [wiki]Thread:start[/wiki] for instance.
Oh, man, I didn't realise this was possible! That solves all kinds of problems.
Same here. Awesome, this will do. And yes, that was what I was looking for.

Together with the table method you mentioned, this should solve all my problems.
Thanks!

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sun Feb 02, 2014 8:14 am
by Germanunkol
Follow-up question:

Is there a way to get the number of currently running threads in 0.9.0?

I used to just do:
#love.thread.getThreads()
but that function got removed...

Re: Thread to thread communication in Löve 0.9.0 - Discussio

Posted: Sun Feb 02, 2014 8:36 am
by slime
Not without keeping your own list of the threads you've created - which you might want to do anyway.