Multi-threaded exception handling

Last week we explored the possibilities of using multi-threading to create animated – or even interactive – loading screens, and drastically decrease loading times.

While we went into a lot of details, using the loading screen of Roche Fusion as an example, there is one important topic we glanced over completely:

What if something goes wrong?

Or more technically: what if one of our threads throws an exception?

Continue reading →

Using multi-threading to animate and speed up loading screens

One feature that almost all games have in common is loading screens.

Many small games can get away with a single loading screen at the start of the game to load all or most assets.

Most larger games have one before every level or environment, or at least for major transitions. The only way to get around loading screens entirely is to have only a small core engine that is loaded at the beginning of the game, and to then load everything else (other engine parts and frameworks, assets, script files..) on the fly.

To make the latter feasible without causing the game to freeze at times, concurrency is a necessity – content has to be loaded in the background while different threads update the gamestate, render it, and keep the interface responsive.

This is a complex topic however, and too big to tackle in this post.

Instead we will look at a smaller problem and how to solve it.

Continue reading →

Scheduling cross-threaded tasks using .Net’s BlockingCollection

Concurrency has always been a complicated aspect of computer science.

With modern hardware increasing not only in speed but also in parallelism, it has become necessary to use concurrency in applications to fully exploit available resources.

This is done using threads, which can be thought of as independently running sub-programs of a larger program, or process. Each thread has its own state – including its stack – and executes its own code, but stacks within the same process can generally share other resources at will.

With even the most simple applications being multi-threaded these days, games are no exception. As such, one can hardly find a modern game engine or framework that does not use parallelism in some form or another.

However, today I do not want to talk about how to move complex systems like physics simulation, AI, pathfinding, or even rendering to different threads – like many games do.

Instead I will look at a specific low-level problem:

Assuming we have a multi-threaded program, how can the different threads communicate, and specifically: how can we make sure that certain code is always run on certain threads.

Continue reading →