Unformatted text preview: ﬁrst column, the notation Ú Ø denotes an instance of variable Ú residing on the local stack for thread Ø, where Ø is either m (main thread), p0 (peer thread 0), or p1 (peer thread 1). Variable instance ptr cnt i.m msgs.m myid.p0 myid.p1 Referenced by main thread? Referenced by peer thread 0 ? Referenced by peer thread 1? 11.4. SYNCHRONIZING THREADS WITH SEMAPHORES
B. Given the analysis in Part A, which of the variables ptr, cnt, i, msgs, and myid are shared? 573 11.4 Synchronizing Threads with Semaphores
Shared variables can be convenient, but they introduce the possibility of a new class of synchronization errors that we have not encountered yet. Consider the badcnt.c program in Figure 11.8 that creates two threads, each of which increments a shared counter variable called cnt. Since each thread increments the counter NITERS times, we might expect its ﬁnal value to be ¾ ¢ NITERS. However, when we run badcnt.c on our system, we not only get wrong answers, we get different answers each time! unix> ./badcnt BOOM! ctr=198841183 unix> ./badcnt BOOM! ctr=198261801 unix> ./badcnt BOOM...
View Full Document