Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main-deadlock-global.c 764B

12345678910111213141516171819202122232425262728293031
  1. #include <stdio.h>
  2. #include "mythreads.h"
  3. pthread_mutex_t g = PTHREAD_MUTEX_INITIALIZER;
  4. pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
  5. pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
  6. void* worker(void* arg) {
  7. Pthread_mutex_lock(&g);
  8. if ((long long) arg == 0) {
  9. Pthread_mutex_lock(&m1);
  10. Pthread_mutex_lock(&m2);
  11. } else {
  12. Pthread_mutex_lock(&m2);
  13. Pthread_mutex_lock(&m1);
  14. }
  15. Pthread_mutex_unlock(&m1);
  16. Pthread_mutex_unlock(&m2);
  17. Pthread_mutex_unlock(&g);
  18. return NULL;
  19. }
  20. int main(int argc, char *argv[]) {
  21. pthread_t p1, p2;
  22. Pthread_create(&p1, NULL, worker, (void *) (long long) 0);
  23. Pthread_create(&p2, NULL, worker, (void *) (long long) 1);
  24. Pthread_join(p1, NULL);
  25. Pthread_join(p2, NULL);
  26. return 0;
  27. }