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.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Love threads?

Post by zac352 »

http://love2d.org/wiki/love.thread.newThread
Data and files are the only things accepted. :huh:
What is data?
Last edited by zac352 on Sat Oct 16, 2010 1:07 am, edited 1 time in total.
Hello, I am not dead.
giniu
Party member
Posts: 221
Joined: Mon Nov 30, 2009 4:44 pm

Re: Love threads?

Post by giniu »

So what? Is this a question? Statement? Complain? Feature request? Even spamming bots add some more context from time to time - give others a chance, and spell what's on your mind. Anyway, let's assume it was a question, someone else might be interested in answer.

Yes, as documentation says - only File, FileData and string representing a file name are accepted (documentation skips it I think, but it works - you can check in background loading sample I made some time ago, just when threads were re-enabled in 0.7 branch).

Anyway, who needs more? Message passing concurrency is the way to go, look at how well Erlang behaves! Naked shared state, mutexes and semaphores are for bug fans and kids, so they can play with their electric train toy :P KISS and it is just simpler that way. The fact that you put code in separate file don't tempt you to use something from outside of thread which would make no much sense - and you split the code into multiple files anyway.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Love threads?

Post by bartbes »

Giniu, epic post.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Love threads?

Post by zac352 »

giniu wrote:So what? Is this a question? Statement? Complain? Feature request? Even spamming bots add some more context from time to time - give others a chance, and spell what's on your mind. Anyway, let's assume it was a question, someone else might be interested in answer.

Yes, as documentation says - only File, FileData and string representing a file name are accepted (documentation skips it I think, but it works - you can check in background loading sample I made some time ago, just when threads were re-enabled in 0.7 branch).

Anyway, who needs more? Message passing concurrency is the way to go, look at how well Erlang behaves! Naked shared state, mutexes and semaphores are for bug fans and kids, so they can play with their electric train toy :P KISS and it is just simpler that way. The fact that you put code in separate file don't tempt you to use something from outside of thread which would make no much sense - and you split the code into multiple files anyway.
Did you just write a short speech about how my thread sucks? :death:
Hello, I am not dead.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Love threads?

Post by thelinx »

Yes, and you better read it.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Love threads?

Post by zac352 »

thelinx wrote:Yes, and you better read it.
I don't usually tl;dr unless the post is about 100 lines long.. :P
Hello, I am not dead.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Love threads?

Post by Jasoco »

What exactly is "data" in reference to anyway? What constitutes "data"?
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Love threads?

Post by zac352 »

Jasoco wrote:What exactly is "data" in reference to anyway? What constitutes "data"?
... That's pretty much what I asked, then got a hate post about. You're brave. :o
Hello, I am not dead.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Love threads?

Post by Jasoco »

Well I know what a file is. But what is data? This is a serious question. Can I send a function into a separate thread? Can threads talk to each other? I'm still confused and intrigued about them.

Also, you didn't really ask a question. You stated a fact. You should have said "What is Data?"
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Love threads?

Post by nevon »

Jasoco wrote:What exactly is "data" in reference to anyway? What constitutes "data"?
You can't really send a function. You can, however, send a file containing a function (that is hopefully then also called). You can either do this this easy way by simply making a separate file where you do what you want to do, or you can do this weird shit that bartbes did:

Code: Select all

local pollservthread = love.thread.newThread("pollserver", love.filesystem.newFileData(([[
require "socket"
sock = socket.udp()
t = love.thread.getThread()
while true do
	sip = t:demand("ip")
	sport = t:demand("port")
	info = "%s"
	sock:sendto("%s", sip, sport)
	sock:settimeout(2)
	local data, ip, port = sock:receivefrom()
	if not data then return t:send("name", false) end
	if data:sub(1, #info) == info and ip == sip then
		local name, version, additional = data:gmatch(info .. ":([^:]*):([^:]*):(.*)")()
		local args = {}
		for s in additional:gmatch("([^:]*)") do
			table.insert(args, s)
		end
		if name and version then
			t:send("name", name)
			t:send("version", version)
			--t:send("args", args)
		end
	end
	t:send("name", false)
end
]]):format(servbrowser.info, servbrowser.poll), "pollserver.lua"))
Don't ask me how it works or what the syntax for love.filesystem.newFileData is - because I don't know. However, it works.

Also, don't forget to do thread:start() after creating a thread. That tripped me up quite a bit. :P

As for threads talking to each other, yes they can. They can use message passing, using the methods: peek, demand, receive, and send.

I can write a small example below. The first file is our main thread, and the other is the thread we're creating.

Code: Select all

local t = love.thread.newThread("workhorse", "filebelow.lua") --Creating our thread and telling it to run the code from the file filebelow.lua
t:start()

function love.update(dt)
    local answer = t:receive("answer") -- The receive method tries to get a message called "answer" from the thread, but only if it's ready. Otherwise it just returns nil
    if answer then
        print("We've solved the mystery of life! It's "..answer)
    end
end

Code: Select all

local t = love.thread.getThread() -- Returns the current thread object
local answer = 21*2
t:send("answer", answer) -- Now that we have our answer, we send it back as a message called "answer".
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 58 guests