Help with threading?

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.
Post Reply
tangletail
Prole
Posts: 1
Joined: Thu Oct 15, 2015 6:06 am

Help with threading?

Post by tangletail »

I am not new to programming, I've been working with C++ and threading in the language for some time. But when I began to try out threading with Love, it just takes me out for a spin.

The general concept is to get a ton of entities to update on the screen all within a reasonable amount of time. I'd like to approach this via a job-based threading method. But the documentation isn't really providing much information on how to pass around data. Or any kind of semaphor for that matter (Waiting for all entities to finish updating before moving on).

Does anyone have any information on the matter? Also an example of a system in a game would be preferable.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Help with threading?

Post by bobbyjones »

Bartbes' async lib could be useful for that.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Help with threading?

Post by Germanunkol »

This might come a little late, but the general Idea I use is the following (someone on this forum once gave me this hint):
In the main thread:
- Create two channels, channelIn and channelOut
- Create the thread
- Call thread:start and pass channelIn and channel Out to it: thread:start( channelIn, channelOut ). This makes sure that the first two agruments the thread receives are the communicating channels.
- Start sending stuff on channelIn and wait for responses on channelOut*

In the created thread:
-

Code: Select all

local args = { ... }; local channelIn = args[1]; local channelOut = args[2]
- Start listening for commands on channelIn and send responses on channelOut.

The way I often send packets is to create a small table, with a "key" and a "value", where the key identifies what type of packet it is. For example, when I want to notify the main thread that something went wrong, I would do something like:

Code: Select all

channelOut:push( {key="error", value="Connection timed out"}).
I use this method in trAInsported for connection handling, for example for the client. There are two (somewhat uncommented, sorry) scripts: connectionThreadClient.lua, which is the background thread handling connections (on the client) and the connectionClient.lua, the foreground thread which handles it.

*It's also very important to start listening to connectionThread:getError(), because otherwise you'll never get syntax errors etc. from your thread.
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: Help with threading?

Post by bartbes »

Germanunkol wrote: In the created thread:
-

Code: Select all

local args = { ... }; local channelIn = args[1]; local channelOut = args[2]
Or without the table:

Code: Select all

local channelIn, channelOut = ...
Germanunkol wrote: *It's also very important to start listening to connectionThread:getError(), because otherwise you'll never get syntax errors etc. from your thread.
Alternatively, use love.threaderror.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 9 guests