Multithreading Prototypes

I started thinking about how to implement multi-threading in a game engine.  My idea is that if every subsystem is independent from each other one, then the updates can be done in parallel (so, each subsystem does its tick in a separate thread).  It’s then up to the higher level system to collect and distribute information between threads as necessary, which would only be done when the threads aren’t executing.

I’ve created a demo showing two ways this can be done:  one with a fork join for each tick, which is sort of slow because of all of the overhead of all those fork joins (and that’s on Linux; I can’t imagine how slow Win32 would go), and a second that uses semaphores to make sure that the frames are synced between threads, which has virtually zero overhead.

The SCons project is set up to compile the semaphore version.  SDL is required as SDL threads are used.  This should be completely portable to any system that SDL runs on.

The command-line arguments are:

./test-semaphores [multithreading] [outerloop] [innerloop] [tasks]

multithreading can be yes or no: yes means use multiple threads to execute each task in parallel (one thread per task), no means execute each task serially — default is no

outerloop is an integer: how many times to do a tick — default is 100

innerloop is an integer: how many loops each task does, to emulate the time a real task (subsystem tick) would take — default is 10000

tasks is an integer: how many subsystem ticks we emulate — default is 3

Here’s a link to the prototype

Comments are closed.

Dev Journal and Project Hosting