Information Technology Reference
In-Depth Information
Suppose, for example, we had a primitive called a lock that ensures that only
one thread at a time can own a lock. Then, we can solve the too much milk
problem by defining the class for a Kitchen object with the following method:
Kitchen::buyIfNeeded(){
lock.acquire();
if(milk==0){ //ifnomilk
milk++; //buymilk
}
lock.release();
}
We will define locks and condition variables (another type of synchronization
object) in the next section.
Exercises
1. Show that solution 3 to the Too Much Milk problem is safe|that it guar-
antees that at most one roommate buys milk.
5.2
Shared objects and synchronization variables
The above discussion should convince you that it is unappealing to try to
write multi-threaded programs using just atomic loads and stores. Fortunately,
decades of work have developed a much simpler approach that extends the
Threads
Shared Objects
State
Variables
Synchronization
Variables
Figure5.1: In a multi-threaded program, threads are separate from and op-
erate on shared objects.
 
Search WWH ::




Custom Search