Information Technology Reference
In-Depth Information
In this function, waitThenDrink() should (if there is no milk) wait (using
a condition variable) until there is milk, drink the milk, and if the milk is
now gone, return a nonzero value to flag that the caller should buy milk.
BuyMilk() should buy milk and then broadcast to let the waiting threads
know that they can proceed.
Again, test your code with varying numbers of threads.
13. Before entering a priority critical section, a thread calls PriorityLock::enter(priority)
and when the thread exits such a critical section it calls PriorityLock::exit() .
If several threads are waiting to enter a priority critical section the one
with the numerically highest priority should be the next one allowed in.
Implement PriorityLock using monitors (locks and condition variables)
and following the multi-threaded programming standards defined for the
class.
(a) Define the state and synchronization variables and describe the pur-
pose of each.
(b) Implement PriorityLock::enter(intpriority)
(c) Implement PriorityLock::exit()
5.7
Conclusions
Using well-structured shared objects to share state among threads makes rea-
soning about multithreaded programs vastly simpler than it would be if we tried
to reason about the possible interleavings of individual loads and stores.
Furthermore, if we follow a systematic approach, it is not dicult to write
code for shared objects that is easy for us to reason about and for others to
read, understand, maintain, and change.
In short, this chapter defines a set of core skills that almost any programmer
will use over and over again during the coming decade or longer.
That is not the whole story. As the next chapter will discuss, as systems grow
to include many shared objects and threads, new challenges arise: synchronizing
operations that span multiple shared objects, avoiding deadlocks in which a
set of threads are all waiting for each other to do something, and maximizing
performance when large numbers of threads are contending for a single object.
Sadly, solutions to these problems are not as cut and dried.
Search WWH ::




Custom Search