Game Development Reference
In-Depth Information
philosophers. Even-numbered philosophers should attempt to pick up their left chop-
stick first, and odd-numbered philosophers must pick up their right chopstick first. If
they can
t acquire both chopsticks, they must relinquish the one they have and try
again later. This solution, and those to other interesting problems of this sort, can
be found in Andrew Tannenbaum
'
s book, Modern Operating Systems.
If you find yourself at a table with four other people and only five chopsticks between
you, simply agree to pick up the left chopstick first and the right chopstick second.
When you are ready to stop eating and start thinking, put them down in reverse
order. Believe it or not, no deadlock will happen, and no one will starve.
There are a number of these interesting problems, which you should look up and try
to solve on your own:
'
n Cigarette smokers
problem
n Sleeping barbers ' problem
n Dining cryptographers
'
'
protocol
Thread Safety
As you might imagine, there are often more things you shouldn
'
t do in a thread than
you should. For one thing, most STL and ANSI C calls are not thread safe. In other
words, you can
t manipulate the same std::list or make calls to fread() from
multiple threads without something bad happening to your program. If you need to
do these things in multiple concurrent threads, either you need to use the thread safe
equivalent of these calls or you need to manage the calls with critical sections. A good
example of this is included in the GameCode4 source code, which manages any
std::basic_ostream< char_type, traits_type> and allows you to safely
write to it from multiple threads. Look in the Multicore\SafeStream.h file for the tem-
plate class and an example of how it can be used.
'
Multithreading Classes in GameCode4
You are ready to see how these concepts are put to work in the GameCode4 architec-
ture. There are two systems that make this easy: the Process Manager and the Event
Manager. If you recall from Chapter 7, Controlling the Main Loop, the Process-
Manager is a container for cooperative processes that inherit from the Process
class. It is simple to extend the Process class to create a real-time version of it,
and while the operating system manages the thread portion of the class, the data
and existence of it are still managed by the ProcessManager class. This turns out
to be really useful, since initialization and process dependencies are still possible,
even between normal and real-time processes.
 
 
 
Search WWH ::




Custom Search