This preview shows page 1. Sign up to view the full content.
Unformatted text preview: o memory? (3) And ﬁnally, how many threads reference each of these instances? The variable is shared if and only if multiple threads reference some instance of the variable. To keep our discussion of sharing concrete, we will use the program in Figure 11.7 as a running example. Although somewhat contrived, it is nonetheless useful to study because it illustrates a number of subtle points about sharing. The example program consists of a main thread that creates two peer threads. The main thread passes a unique ID to each peer thread, which uses the id to print a personalized message, along with a count of the total number of times that the thread routine has been invoked. Here is the output when we run it on our system:
unix> ./sharing : Hello from foo (cnt=1) : Hello from bar (cnt=2) 11.3.1 Threads Memory Model
A pool of concurrent threads runs in the context of a process. Each thread has its own separate thread context, which includes a thread ID, stack, stack pointer, program counter, condition codes, and generalpurpose register values. Each thread shares the rest of the process context with the other threads. This includes the entire user virtual address space, which consists of read-only text (code), read/write data, the heap, and any shared library cod...
View Full Document
- Spring '10
- The American