Suppose having stuff drawn that shuld be calculated on another thread.
My intuition would be like(semi-pseudocode all the way):
Code: Select all
OBJECTS = {<arbitrarylist of drawable objects and depending data>}
function love.draw()
for k,DATA in ipairs(OBJECTS) do
love.gr.draw(DATA.object,unpack(DATA.data)) --DATA.data = {[1] = x,[2] = y, [3] = rot, [4] = scale_x, [5] = scale_y}
end
end
--function to be replaced:
function love.update()
for k,DATA in ipairs(OBJECTS) do <-- this should be outsoured
modify(DATA.data) --do some stuff with data in that table like updating position
end
end
--to outsource i would just do like:
function love.update()
thread.assignto(2) --pseudofunction to assign stuff to other thread;1 being the mainthread
for k,DATA in ipairs(OBJECTS) do <-- this should not be calculated simultaneously on the 2nd thread
modify(DATA.data)
end
thread.endassign() --pseudofunction to declare that the assignment of a function is limited to this point
end
(besides using non-existent functions and logic)
(https://love2d.org/wiki/love.thread)However, as they are separate environments, they cannot access the variables and functions of the main thread, and communication between threads is limited.
Also the thread of examples (https://love2d.org/forums/viewtopic.php?f=4&t=76670) mentioned on the wiki is already a bit too advanced for me on this topic. :/
Some questions I have:
I get that threads are using dedicated chunks of lua that can't write code to the memory like I needed due to it not necessarily being sync then. So channels come in for communication, or? But how do they work?
Could someone solve my outsourcing problem with working code -if it's not too much work- for me to have a direct comparison of how I'd like it to work vs. how this would actually be possible?
I see functions like channel:push/pop - why is it needed? It implies that channels are stacks(?) or objects in a stack?
The best example that gives me somewhat of an understanding is here: https://love2d.org/wiki/love.thread.getChannel ("Communication between main/thread")
Well but I don't get it - I guess I'ts made to print "foo" and "bar" forever from different threads when the other thread wrote something to he other channel.