It is unfortunate that the pthreads standards group

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: s reference the contents of the main thread’s stack indirectly through the global ptr variable. 11.3.2 Mapping Variables to Memory C variables in threaded programs are mapped to virtual memory according to their storage classes. 11.3. SHARED VARIABLES IN THREADED PROGRAMS 571 code/threads/sharing.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include "csapp.h" #define N 2 char **ptr; /* global variable */ void *thread(void *vargp); int main() { int i; pthread_t tid; char *msgs[N] = { "Hello from foo", "Hello from bar" }; ptr = msgs; for (i = 0; i < N; i++) Pthread_create(&tid, NULL, thread, (void *)i); Pthread_exit(NULL); } void *thread(void *vargp) { int myid = (int)vargp; static int cnt = 0; printf("[%d]: %s (cnt=%d)\n", myid, ptr[myid], ++cnt); } code/threads/sharing.c Figure 11.7: Example program that illustrates different aspects of sharing. 572 CHAPTER 11. CONCURRENT PROGRAMMING WITH THREADS ¯ Global variables. A global variable is any variable declared outside of a function. At run-time, the read/write area of virtual memory contains exactl...
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