Created
December 24, 2019 00:37
-
-
Save swarajd/a4f3cc000bfaa15d27e8d7e74a911f7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "mythreads.h" | |
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER; | |
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER; | |
void* worker(void* arg) { | |
if ((long long) arg == 0) { | |
Pthread_mutex_lock(&m1); | |
Pthread_mutex_lock(&m2); | |
} else { | |
Pthread_mutex_lock(&m2); | |
Pthread_mutex_lock(&m1); | |
} | |
Pthread_mutex_unlock(&m1); | |
Pthread_mutex_unlock(&m2); | |
return NULL; | |
} | |
int main(int argc, char *argv[]) { | |
pthread_t p1, p2; | |
Pthread_create(&p1, NULL, worker, (void *) (long long) 0); | |
Pthread_create(&p2, NULL, worker, (void *) (long long) 1); | |
Pthread_join(p1, NULL); | |
Pthread_join(p2, NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment