Game Development Reference
In-Depth Information
Multithreaded programming
Normally, a program has a single thread of execution. A thread is basically code that
is being run by one of the cores in a system that has multi-core processors. Most
modern processors have this feature, though it should be noted that you can still have
multiple threads on a single-core CPU. I wanted to include this topic because multith-
reading is very useful in processor-intensive applications such as video games.
Basically, multithreading lets you have more than one point of execution running in
your application at the same time. So, for example, you could have one thread doing
some background work such as loading resources (graphics and sounds) in a large
world similar to those found in Minecraft . As the player moves, the game has to load
in the next chunks of the world, and unload the ones that are now too far behind the
player. So, you could have one thread that handles loading new world chunks as the
player moves around in the world, while the main thread keeps running the game
code.
Of course, you are not limited to just two threads either. It is common for applications
to create a group of threads and assign jobs to them. This is handy if there are numer-
ous tasks that need to be done. You just make a thread manager class, and pass in
the function that you will call to do the job that needs to be done. The thread manager
will look at its threads (collectively known as a thread pool ) and find the one that is
idle. If it finds an idle worker thread , it will start it by running the function you passed
in. A worker thread is just a thread that is a member of a thread pool, and it gets its
name since it does various jobs for us.
Running multiple bits of code simultaneously like this can obviously have significant
performance benefits. However, multithreaded programming is also a rather tricky
subject. You have to make sure that you don't have two threads accessing the same
data at the same time, as this can cause big problems.
Search WWH ::




Custom Search