How works love.threads?

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.
NoAim91
Prole
Posts: 36
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

How works love.threads?

Post by NoAim91 » Fri Jun 16, 2017 1:05 pm

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))

User avatar
zorg
Party member
Posts: 2720
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How works love.threads?

Post by zorg » Fri Jun 16, 2017 1:31 pm

Anything you pass into Thread:start will be available as a "file-level" vararg that you can access by doing this:

Code: Select all

local stuff = {...}
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))

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.

NoAim91
Prole
Posts: 36
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: How works love.threads?

Post by NoAim91 » Fri Jun 16, 2017 2:30 pm

@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

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How works love.threads?

Post by Nixola » Fri Jun 16, 2017 2:32 pm

wiki wrote:Unless you define the love.threaderror callback or call Thread:getError you won't see any errors your thread code throws.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

NoAim91
Prole
Posts: 36
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: How works love.threads?

Post by NoAim91 » Fri Jun 16, 2017 3:04 pm

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

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How works love.threads?

Post by bartbes » Fri Jun 16, 2017 3:18 pm

Yes, because input is nil, change your first line to

Code: Select all

local input = ...

NoAim91
Prole
Posts: 36
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: How works love.threads?

Post by NoAim91 » Fri Jun 16, 2017 3:25 pm

thank you! :-)

Ok, now how I get the resault ( a ) out of the thread? XD

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How works love.threads?

Post by bartbes » Fri Jun 16, 2017 3:44 pm

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.

NoAim91
Prole
Posts: 36
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: How works love.threads?

Post by NoAim91 » Fri Jun 16, 2017 3:56 pm

@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

User avatar
zorg
Party member
Posts: 2720
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How works love.threads?

Post by zorg » Fri Jun 16, 2017 4:02 pm

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...
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests