Last active
May 31, 2018 12:22
-
-
Save ccckmit/7bbb0d116f6ca2506b8674b799e9621c 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 <pthread.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
pthread_mutex_t x; | |
pthread_mutex_t y; | |
void *A(); | |
void *B(); | |
int main(int argc, char *argv[]) | |
{ | |
pthread_t threadA, threadB; | |
pthread_attr_t attr; | |
pthread_attr_init(&attr); | |
pthread_mutex_init(&x, NULL); | |
pthread_mutex_init(&y, NULL); | |
pthread_create(&threadA, &attr, A, NULL); | |
pthread_create(&threadB, &attr, B, NULL); | |
pthread_join(threadA, NULL); | |
pthread_join(threadB, NULL); | |
pthread_mutex_destroy(&x); | |
pthread_mutex_destroy(&y); | |
} | |
void *A() | |
{ | |
pthread_mutex_lock(&x); | |
printf("A lock x\n"); | |
pthread_mutex_lock(&y); | |
printf("A lock y\n"); | |
pthread_mutex_unlock(&y); | |
pthread_mutex_unlock(&x); | |
printf("finished A\n"); | |
pthread_exit(0); | |
} | |
void *B() | |
{ | |
pthread_mutex_lock(&y); | |
printf("B lock y\n"); | |
pthread_mutex_lock(&x); | |
printf("B lock x\n"); | |
pthread_mutex_unlock(&x); | |
pthread_mutex_unlock(&y); | |
pthread_exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment