/* * mutex.c: demonstration of mutex * * CS50, Spring 2022 */ #include #include #include #include #include /********** global variable *******/ pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; /********** function prototype *******/ void print(char* a, char* b); void* print_i(void *ptr); void* print_j(void *ptr); /********** main *******/ int main() { pthread_t t1, t2; int iret1 = pthread_create(&t1, NULL, print_i, NULL); int iret2 = pthread_create(&t2, NULL, print_j, NULL); if (iret1 || iret2) { fprintf(stderr,"pthread_create failed: iret1=%d, iret2=%d\n", iret1, iret2); exit(1); } else { sleep(8); } exit(0); //never reached. } // try uncommenting and commenting the mutext below // and look at the output void print(char* a, char* b) { pthread_mutex_lock(&mutex1); // comment out printf("1: %s\n", a); sleep(1); printf("2: %s\n", b); pthread_mutex_unlock(&mutex1); // comment out } // These two functions will run concurrently. void* print_i(void *ptr) { print("I am", " in i"); return NULL; } void* print_j(void *ptr) { print("I am", " in j"); return NULL; }