returning table from love.thread

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.
Post Reply
User avatar
farzher
Prole
Posts: 41
Joined: Fri Jul 31, 2015 5:38 pm

returning table from love.thread

Post by farzher »

Do I have to serialize my table as some kind of string or whatever then unserialize it in my main thread?

There's no way to pass a reference to a table in a thread back to the main process? Or pass a reference to a table into the thread for it to edit?

The whole point of my thread is to generate a huge table, deserializing in the main thread will lag me
butts
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: returning table from love.thread

Post by zorg »

Channel:push mentions (along with Channel:supply for that matter) that the allowed types you can send are what are described in the Variant article. In other words, flat tables are also supported. If your tables aren't flat, then yes, you'd need to serialize/flatten them in some way.

But no, the data will be copied in all cases... FFI might allow you to create a memory region you could (ab)use by sending its pointer to another thread as a number or string... but i faintly recall me being told that that may not work either; haven't tested it yet so i can't give a concrete answer.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: returning table from love.thread

Post by s-ol »

zorg wrote: Sun Feb 12, 2017 6:04 am Channel:push mentions (along with Channel:supply for that matter) that the allowed types you can send are what are described in the Variant article. In other words, flat tables are also supported. If your tables aren't flat, then yes, you'd need to serialize/flatten them in some way.

But no, the data will be copied in all cases... FFI might allow you to create a memory region you could (ab)use by sending its pointer to another thread as a number or string... but i faintly recall me being told that that may not work either; haven't tested it yet so i can't give a concrete answer.
Im not sure if ffi allocated memory works but you can just use a FileData and getPointer()

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: returning table from love.thread

Post by zorg »

s-ol wrote: Sun Feb 12, 2017 10:29 pm Im not sure if ffi allocated memory works but you can just use a FileData and getPointer()
Technically, both pointers would be numbers, so it should work for both cases, the difference has to do with whether FileData gets duplicated (one copy per thread) or it's only ever just one instance being "shared" across threads (if it's the duping case, getPointer is kinda worthless). Gonna test that out tomorrow.

Edit: After testing and actually finding one relevant sentence at the top of the love.thread article on the wiki, s-ol is right in löve userdata being shared across threads; one only needs to send their references.
Last edited by zorg on Tue Feb 14, 2017 1:09 am, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: returning table from love.thread

Post by s-ol »

zorg wrote: Sun Feb 12, 2017 11:29 pm
s-ol wrote: Sun Feb 12, 2017 10:29 pm Im not sure if ffi allocated memory works but you can just use a FileData and getPointer()
Technically, both pointers would be numbers, so it should work for both cases, the difference has to do with whether FileData gets duplicated (one copy per thread) or it's only ever just one instance being "shared" across threads (if it's the duping case, getPointer is kinda worthless). Gonna test that out tomorrow.
I'm pretty sure the metadata (pointer value) gets duped but the memory is the same. An ImageData is basically a FileData too and thats how you preload images in a different thread / make a loading animation.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: returning table from love.thread

Post by raidho36 »

Both ImageData and FIleData inherit from base class Data. They're not the same, and their respective cloning functions also differ.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: returning table from love.thread

Post by s-ol »

raidho36 wrote: Mon Feb 13, 2017 7:30 am Both ImageData and FIleData inherit from base class Data. They're not the same, and their respective cloning functions also differ.
I don't see a cloning function in either implementation, and love.event just uses a Variant which holds a simple pointer as far as I can tell.

https://bitbucket.org/rude/love/src/150 ... ew-default
https://bitbucket.org/rude/love/src/150 ... ew-default

the Mutex in ImageData is only used to implement mapPixel thread-safely to guarantee it is atomic.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
farzher
Prole
Posts: 41
Joined: Fri Jul 31, 2015 5:38 pm

Re: returning table from love.thread

Post by farzher »

That's all very interesting, but also very confusing. If it's possible could someone put a simple example code for it?

I tried for now just sending serialized table data back, but I'm sometimes getting a stackoverflow error in the thread when calling channel:push for an unknown reason. and other times channel:deamnd freezes forever. I assume it's because I'm trying to send a 200,000 byte string (although I tried chunking it up into small pieces and still got errors)
butts
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 38 guests