When getchar returns the peer thread signals the main

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: When we reason about concurrent execution, the only assumption we can make about the total ordering of instructions is that it is sequentially consistent. That is, instructions 574 CHAPTER 11. CONCURRENT PROGRAMMING WITH THREADS code/threads/badcnt.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 35 #include "csapp.h" #define NITERS 100000000 void *count(void *arg); /* shared variable */ unsigned int cnt = 0; int main() { pthread_t tid1, tid2; Pthread_create(&tid1, NULL, count, NULL); Pthread_create(&tid2, NULL, count, NULL); Pthread_join(tid1, NULL); Pthread_join(tid2, NULL); if (cnt != (unsigned)NITERS*2) printf("BOOM! cnt=%d\n", cnt); else printf("OK cnt=%d\n", cnt); exit(0); } /* thread routine */ void *count(void *arg) { int i; for (i=0; i<NITERS; i++) cnt++; return NULL; } code/threads/badcnt.c Figure 11.8: badcnt.c: An improperly synchronized counter program. 11.4. SYNCHRONIZING THREADS WITH SEMAPHORES Asm code for thread i .L9: movl...
View Full Document

Ask a homework question - tutors are online