Page 1 of 2
How works love.threads?
Posted: Fri Jun 16, 2017 1:05 pm
by NoAim91
Hey, the love.thread thing really need a bit more description at the wiki. I like to input a variable in the thread and get a return after a function call. In this case I put 10 in, the function should add 3 and the return should be 13. But the function inside the thread doesn´t get called. and in generell I have no clue what is going on.
main.lua
Code: Select all
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
t:start(c1:push(input))
table = c1:demand()
print(table)
end
thread.lua
Code: Select all
function test(input)
a = 3
a = a + input
print(a)
return a
end
input = c1:demand()
c1:push(test(input))
Re: How works love.threads?
Posted: Fri Jun 16, 2017 1:31 pm
by zorg
Anything you pass into Thread:start will be available as a "file-level" vararg that you can access by doing this:
for example; you can also use the select function too.
Haven't tested the following, but it may or may not work:
Code: Select all
-- main.lua
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
t:start(c1)
c1:push(input)
table = c1:demand()
print(table)
end
Code: Select all
--thread.lua
local c1 = {...}[1]
function test(input)
a = 3
a = a + input
print(a)
return a
end
input = c1:demand()
c1:push(test(input))
Re: How works love.threads?
Posted: Fri Jun 16, 2017 2:30 pm
by NoAim91
@zorg: nope, doesn´t work.
Maybe I don´t need channels for this.
I tryed this
Code: Select all
--main.lua
function love.load()
t = love.thread.newThread("thread.lua")
input = 10
output = t:start( input )
print(output)
end
Code: Select all
--thread.lua
local input = input
function test(input)
a = 3
a = a + input
print(a)
return a
end
test(input)
and this did not work.
print(output) --> true
print(a) --> doesn´t print anything
Re: How works love.threads?
Posted: Fri Jun 16, 2017 2:32 pm
by Nixola
Re: How works love.threads?
Posted: Fri Jun 16, 2017 3:04 pm
by NoAim91
Code: Select all
local input = input
function test(input)
a = input
a = a + 3
print(a)
return a
end
test(input)
Thread error!
thread.lua:5: attempt to perform arithmetic on global 'a' (a nil value)
line 5 is a = a + 3
so what?! define a = input is fine but after that a is unknown?! o.O
Re: How works love.threads?
Posted: Fri Jun 16, 2017 3:18 pm
by bartbes
Yes, because input is nil, change your first line to
Re: How works love.threads?
Posted: Fri Jun 16, 2017 3:25 pm
by NoAim91
thank you! :-)
Ok, now how I get the resault ( a ) out of the thread? XD
Re: How works love.threads?
Posted: Fri Jun 16, 2017 3:44 pm
by bartbes
The easiest way is using a channel, so if we take your initial code again...
Code: Select all
-- main.lua
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
c1:push(input)
t:start(c1)
table = c1:demand()
print(table)
end
-- thread.lua
local c1 = ...
function test(input)
a = 3
a = a + input
print(a)
return a
end
input = c1:demand()
c1:push(test(input))
Of course you probably don't want to use threads for this, but it should work.
Re: How works love.threads?
Posted: Fri Jun 16, 2017 3:56 pm
by NoAim91
@bartbes: this code doesn´t work :-/
I changed something that it work, but it is not finaly right:
Code: Select all
main.lua
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
c1:push(input)
t:start(c1)
end
Code: Select all
thread.lua
local c1 = ...
input = c1:demand()
print("thread/ input: "..input) -- print 10
function test(input)
a = input
a = a + 3
print("thread/ a: "..a) -- print 13
return a
end
c1:push(test(input))
and the strange thing is, when I write this:
Code: Select all
function love.load()
t = love.thread.newThread("thread.lua")
c1 = love.thread.newChannel()
input = 10
c1:push(input)
t:start(c1)
output = c1:demand()
print("main/ output: "..output) --print 10
end
it only print 10 ... and nothing from the thread ... and the thread gives no error
Re: How works love.threads?
Posted: Fri Jun 16, 2017 4:02 pm
by zorg
So your issue is that the print command doesn't work in the thread? Because it seems like the thread did return 10 to the main thread, and it was printed...