// adapted from ostep 26-threads-intro/t1.c // #include #include #include #include #define Pthread_create(thread,attr,start_routine,arg) \ assert(pthread_create(thread,attr,start_routine,arg) == 0) #define Pthread_join(thread,value_ptr) \ assert(pthread_join(thread,value_ptr) == 0) #define Pthread_mutex_lock(m) \ assert(pthread_mutex_lock(m) == 0) #define Pthread_mutex_unlock(m) \ assert(pthread_mutex_unlock(m) == 0) int max; volatile int counter = 0; // shared global variable void *mythread(void *arg) { char *letter = arg; int i; // stack (private per thread) printf("%s: begin [addr of i: %p]\n", letter, &i); for (i = 0; i < max; i++) { counter = counter + 1; // shared: only one } printf("%s: done\n", letter); return NULL; } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usage: main-first \n"); exit(1); } max = atoi(argv[1]); pthread_t p1, p2; printf("main: begin [counter = %d] [%p]\n", counter, &counter); Pthread_create(&p1, NULL, mythread, "A"); Pthread_create(&p2, NULL, mythread, "B"); // join waits for the threads to finish Pthread_join(p1, NULL); Pthread_join(p2, NULL); printf("main: done\n [counter: %d]\n [should: %d]\n", counter, max*2); return 0; }