Threading the Game Engine

We should not add threads to the game engine unless there is a solid reason for doing so. In a simple demo program, additional threads might slow things down a bit. But in a large, complex game or a demo with many entities, the addition of a thread or two (if done carefully) should reap a significant performance boost. In other words, we want to see the core timing improve or remain steady under load by utilizing additional processor cores.

One issue that crops up when making the paradigm shift to a threaded engine is the problem of lists and iteration, especially when the engine is shutting down. There are many iterations going on in the engine core: moving, animating, drawing, collision testing. Each of these processes involves an iteration at various stages with function calls to game events. If you assume that the game is shutting down and you destroy a mutex while that mutex is in use, it will crash or hang the game. To resolve this problem, every iterative loop must have an added condition that causes it to break out when the game is shutting down. This is done by checking the global gameover flag in every engine function that iterates through the entity list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset