The implications for a programmer are somewhat scary

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: ULL, producer, NULL); Pthread_create(&tid_consumer, NULL, consumer, NULL); Pthread_join(tid_producer, NULL); Pthread_join(tid_consumer, NULL); exit(0); } code/threads/prodcons.c Figure 11.19: Producer-consumer program: Main routine. One producer thread and one consumer thread manipulate a 1-item buffer. Initially, empty == 1 and full == 0. 11.5. SYNCHRONIZING THREADS WITH MUTEX AND CONDITION VARIABLES 585 code/threads/prodcons.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 31 32 33 34 /* producer thread */ void *producer(void *arg) { int i, item; for (i=0; i<NITERS; i++) { /* produce item */ item = i; printf("produced %d\n", item); /* write item to buf */ P(&shared.empty); shared.buf = item; V(&shared.full); } return NULL; } /* consumer thread */ void *consumer(void *arg) { int i, item; for (i=0; i<NITERS; i++) { /* read item from buf */ P(&shared.full); item = shared.buf; V(&shared.empty); /* consume item */ printf("consumed %d\n", item); } return NULL; } code/threads/prodcons.c Figure 11.20: Produ...
View Full Document

Ask a homework question - tutors are online