"Loosing" my 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: "Loosing" my thread?

Post by bartbes » Thu Jan 17, 2013 12:09 pm

Wait, you're running that many threads?!
OS threads are kind of expensive, and they are not cleaned up unless you join them, so you might want to try that, or try cutting back on the amount of threads, and reusing them instead.

User avatar
slime
Solid Snayke
Posts: 2863
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: "Loosing" my thread?

Post by slime » Thu Jan 17, 2013 7:34 pm

"Reduce, reuse, recycle" is especially true for threads.

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: "Loosing" my thread?

Post by Germanunkol » Thu Jan 17, 2013 9:09 pm

This time it happend one match earlier, match ID 248.

It must be something to do with memory or some variable overflowing... or something along those lines... otherwise it wouldn't always happen around the same match...
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: "Loosing" my thread?

Post by Germanunkol » Fri Jan 18, 2013 4:15 pm

Uh, sorry, didn't read the last two posts. Maximum number of threads I ever use is around nineteen, which I know might not be the most efficient solution, but this is only at the first startup, to generate some images.

I use the 249 threads AFTER each other. At the beginning of each round, a thread is started which generates the map. Once the map has been generated, the thread returns using the "return" statement -> last line of the second code box in the first post.
Then I set the pointer pointing to the thread to nil and call the garbage collector.
Does that not join the threads correctly?
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

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

Re: "Loosing" my thread?

Post by bartbes » Fri Jan 18, 2013 4:52 pm

No, you need to actually call join. Also, I was never saying it's bad to have so many threads running at the same time (that too), but I was saying it was bad to have had so many threads.

EDIT: Oops, just remembered the love api calls it 'wait'.

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: "Loosing" my thread?

Post by Germanunkol » Sat Jan 19, 2013 10:49 am

Having other difficulties with my PI right now, I'll report back when I've tried running the server using a proper thread:wait() call. I'm testing it on my other PC right now - I hope this will get me some answers.

Thanks a lot at this point for all the hints and tipps!

@ bartbes I must admit, your last comment left me somewhat confused. I run a server, which has a main thread (for handling other threads and for printout) which generates a connection thread (runs all the time, handling the connection to the clients) and a map-generation thread (which is run whenever a new match is started, finishes after a few second and rejoins). This gameserver needs to run for many days.
But now you say I should not use that many threads... why not? And if not, how else would I handle the game? This is a server, after all. If I do not thread the thing, then the map generation would block all incoming and outgoing connectivity - which defeats the purpose entirely.
During normal operation, the most threads I ever have running at the same time is 3, usually it's only 2 (main and connection). That's standard procedure...?
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

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

Re: "Loosing" my thread?

Post by bartbes » Sat Jan 19, 2013 1:29 pm

Germanunkol wrote: I use the 249 threads AFTER each other.
I assumed (and still am assuming), you mean 249 different threads. That is bad, especially if you don't clean them up properly (though that should hopefully be fixed soon).

See, threads aren't lightweight, so even though it's not bad to have a couple running next to each other (it's what they're for!), you should try and re-use them when they're done, unless their usage is really significantly spaced out. That is, don't stop a thread, to start a new one soon after, if you can avoid it.

Example:
You have a thread, let's call it "clienthandler", that deals with client 1, it does its job for a while, client 1 disconnects, and then clienthandler waits. See, it doesn't quit, it just waits (using demand, most likely, because that's the most efficient) for a new client, when client 4 connects, it gets handed to clienthandler again, which does its thing, repeat.

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: "Loosing" my thread?

Post by Germanunkol » Sun Jan 20, 2013 12:24 am

Ah, thanks! I understand what you're saying... I should re-use the map generation thread and just set it to idle while I don't use it.
I'll think about it, this might actually be quite simple to change...

On my original problem: It seems to be related to the raspberry pi I run the game on. On my laptop I can run around 2000 maps without anything bad happening. I have yet to try the version of the game which joins the threads on the pi, though...
I'll report back when I have more info on that.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: "Loosing" my thread?

Post by Germanunkol » Sat Feb 02, 2013 4:19 pm

Thanks,
It seems to work now. I've now generated 1093 levels and it's still running. All I did was to reuse the old thread, as you suggested. Now it just idles while no map is being generated. As soon as I need a new map, the thread is re-activated but not recreated.
Thanks!
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

Germanunkol
Party member
Posts: 706
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: "Loosing" my thread?

Post by Germanunkol » Thu Feb 21, 2013 5:52 pm

I have another question regarding this.

My thread file ends with this:

Code: Select all

thisThread:set("status", "done")
return
In the main thread, I use the following code to handle the ending of the thread when it's done:

Code: Select all

status = bgBoxThread:get("status")
if status == "done" then
	bgBox = bgBoxThread:get("imageData")		-- get the generated image data from the thread
	bgBox:encode("bgBox.png")
	bgBox = love.graphics.newImage(bgBox)
	bgBoxThread:wait()
	bgBoxThread = nil
end
However, during this process, I check how many threads there still are, using the following code. And this number counts up every time I start a new thread, but it never goes back down when I use the above code to "end" a thread. So after having generated 19 images in 19 threads (and ending all of them), the num variable is still 20 (= 19 plus the main thread).

Code: Select all

t = love.thread.getThreads()
num = 0
for k, v in pairs(t) do
	num = num + 1
end
love.graphics.print(num .. " threads running", 20, 40)
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

Post Reply

Who is online

Users browsing this forum: No registered users and 102 guests