115 synchronizing threads with mutex and condition

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: first 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 final 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

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online