Page 1 of 1

Help with threading?

Posted: Thu Oct 15, 2015 6:15 am
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.

Re: Help with threading?

Posted: Thu Oct 15, 2015 4:51 pm
by bobbyjones
Bartbes' async lib could be useful for that.

Re: Help with threading?

Posted: Thu Oct 22, 2015 5:08 am
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.

Re: Help with threading?

Posted: Fri Oct 23, 2015 6:19 pm
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.