Page 2 of 3
Re: Threads in 0.9.0?
Posted: Wed Dec 03, 2014 3:09 pm
by bartbes
S0lll0s wrote:I think blocking with :demand() on a named channel should do the trick
No reason it has to be named.
S0lll0s wrote:
2. :demand() is implemented in "smart" C++ (vs. long polling), right?
Yes, the thread calling demand is asleep.
S0lll0s wrote:
3. Can I somehow build an atomic "tryDemand" method?
As you already found out, this is (mostly) what pop does.
S0lll0s wrote:If I may only supply flat tables (via Thread.run and channels), how do I make threads cooperatively work on a list of entities?
Push all the entries, and let each thread just take things from the channel until it's empty?
Azhukar wrote:
I believe the limit for number of channels is pretty high.
There is none enforced, so that should be pretty high.
S0lll0s wrote:
Code: Select all
local meta = love.thread.getChannel("meta")
local keys = {}
for k,v in pairs( ent ) do
love.thread.getChannel(k):push(v)
keys[#keys+1] = k
end
meta:supply( keys )
for _,k in ipairs( keys ) do
love.thread.getChannel(k):clear()
end
Why would you not just push all anonymous channels instead, or supply a table of them, instead of their names? Generally speaking you should be using as little named channels as possible (since they're basically globals).
S0lll0s wrote:
Having a way to share actual tables would still be much better.
Unfortunately lua tables can't handle this well at all, which is the reason love has a message-passing based API.
S0lll0s wrote:
And I believe every OS has a method to share RAM between processes (or at least threads)
Memory is shared in threads, always. That said, lua code lives separately, and only love userdata is shared. (Indeed the lua code only ever has a reference, and not the userdata itself.)
Re: Threads in 0.9.0?
Posted: Thu Dec 04, 2014 12:44 am
by Azhukar
bartbes wrote:Push all the entries, and let each thread just take things from the channel until it's empty?
I believe he meant something akin to several threads working over the same array, for example the same 2D map.
Re: Threads in 0.9.0?
Posted: Thu Dec 04, 2014 5:51 am
by s-ol
Azhukar wrote:bartbes wrote:Push all the entries, and let each thread just take things from the channel until it's empty?
I believe he meant something akin to several threads working over the same array, for example the same 2D map.
Yes. Pushing a deep table like a Player entity with position vectors and child entities is neither fun nor performant like this.
Re: Threads in 0.9.0?
Posted: Thu Dec 04, 2014 9:38 am
by bartbes
Then serialise.
Re: Threads in 0.9.0?
Posted: Thu Dec 04, 2014 6:37 pm
by parallax7d
just wondering, are threads and channels really lua lanes and lindas? if so this is cool cause i didn't want to install lanes since it looks like you had to recompile lua, and I didn't know if that version would be compatable with love and still be portable.
Re: Threads in 0.9.0?
Posted: Thu Dec 04, 2014 9:39 pm
by slime
parallax7d wrote:just wondering, are threads and channels really lua lanes and lindas? if so this is cool cause i didn't want to install lanes since it looks like you had to recompile lua, and I didn't know if that version would be compatable with love and still be portable.
No – Channels are custom objects which use a queue data structure and concurrency concepts like condition variables. Thread objects just wrap operating system-level threads and load an independent Lua state.
Re: Threads in 0.9.0?
Posted: Sat Dec 06, 2014 1:35 am
by parallax7d
so threads are like lua threads? concurrent but not parallel? And channels are like lindas, just a seperate os thread with it's own copy of lua? aka parallel but impossible to schedule?
Re: Threads in 0.9.0?
Posted: Sat Dec 06, 2014 6:40 am
by slime
No, love's threads are real operating system threads - concurrent, parallel, and preemptive (when the system is capable.) They aren't really like Lua's coroutines, which have other uses.
Channels aren't their own threads at all, they're queue objects that let you communicate / pass data around between love threads (including making a thread wait for data.)
A typical use-case is to have a love thread and the main thread both using the same Channel, the love thread has code that calculates some value which it pushes to the Channel, and the main thread gets the value via [wiki]Channel:pop[/wiki].
Channel objects have a lot in common with Go's Channels.
Re: Threads in 0.9.0?
Posted: Sat Dec 06, 2014 11:55 am
by s-ol
bartbes wrote:
-snip-
S0lll0s wrote:
And I believe every OS has a method to share RAM between processes (or at least threads)
Memory is shared in threads, always. That said, lua code lives separately, and only love userdata is shared. (Indeed the lua code only ever has a reference, and not the userdata itself.)
So each thread has their own LUA context (or whatever it's called)... Makes sense to prevent race-conditions Inguess but its kind of unfortunate for performance in this manner.
What if there was a table replacement userdata, couldn't you share the reference between threads?
Or a cross-context-reference-userdata? (I don't know the limitations of contexts and userdata)
Something like:
Code: Select all
local deepTable = { a={ 1, 2, 3 } }
local chan = love.thread.get channel("chan")
chan.push( love.thread.newReference( deepTable ) )
-- in code far far away
local var = chan.pop().getValue() -- or even:
local var = chan.pop()
var.a[2] = 13
Basically a userdata wrapper for lua tables or a general value-wrapper that overloads every operation by calling the appropriate action in the other lua context.
Edit: I read up on userdata a tiny bit and it seems you would have to create the reference userdata as the metatable of an empty table and overwrite all metamethods. You of course cannot modify the metatable but that would be a very small limitation.
Re: Threads in 0.9.0?
Posted: Sat Dec 06, 2014 2:36 pm
by parallax7d
thanks slime, that helps clarify things. Also, thank you to the devs for making love threads in the first place, not having to mess with Lanes is so convenient!