// How to use a mutex in C #include #include #include #include #include pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // try uncommenting and commenting the mutext below to see the difference 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 0; } void* print_j(void *ptr) { print("I am", " in j"); return 0; } int main() { pthread_t t1, t2; int iret1 = pthread_create(&t1, NULL, print_i, NULL); int iret2 = pthread_create(&t2, NULL, print_j, NULL); while(1){} exit(0); //never reached. return 0; }