Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3.  
  4. int flag; /* shared variable */
  5.  
  6. pthread_mutex_t m;
  7. pthread_cond_t c;
  8.  
  9. void *threadone(void *arg)
  10. {
  11. pthread_mutex_lock(&m);
  12. while(flag != 1) pthread_cond_wait(&c, &m);
  13. pthread_mutex_unlock(&m);
  14. puts("one");
  15. return(arg);
  16. }
  17. void *threadtwo(void *arg)
  18. {
  19. puts("two");
  20. pthread_mutex_lock(&m);
  21. flag = 1;
  22. pthread_cond_broadcast(&c);
  23. pthread_mutex_unlock(&m);
  24. return(arg);
  25. }
  26.  
  27. int main(void)
  28. {
  29. pthread_t threadid[2];
  30. pthread_mutex_init(&m, 0);
  31. pthread_cond_init(&c, 0);
  32. pthread_create(&threadid[0], 0, threadone, 0);
  33. pthread_create(&threadid[1], 0, threadtwo, 0);
  34. pthread_join(threadid[0], 0);
  35. pthread_join(threadid[1], 0);
  36. pthread_mutex_destroy(&m);
  37. pthread_cond_destroy(&c);
  38. return(0);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement