Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. static int can_go = 0;
  2. static pthread_mutex_t go_mtx = PTHREAD_MUTEX_INITIALIZER;
  3. static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
  4.  
  5. void set_go( void )
  6. {
  7. pthread_mutex_lock( &go_mtx );
  8. can_go = 1;
  9. pthread_cond_signal(&wait_cond);
  10. pthread_mutex_unlock( &go_mtx );
  11. }
  12.  
  13. int wait_go( void )
  14. {
  15. pthread_mutex_lock( &go_mtx );
  16. while(can_go == 0)
  17. {
  18. printf("beging waiting ... n");
  19. pthread_cond_wait(&wait_cond, &go_mtx);
  20. printf("waiting donen");
  21. }
  22. printf("outside of while waiting !!!n");
  23. pthread_mutex_unlock( &go_mtx );
  24. }
  25.  
  26. void *run_thread(viod *ptr)
  27. {
  28. ..............
  29. if (is_sync_thread){ //if this is a special sync thread, do something and trigger other threads
  30. .............
  31. set_go();
  32. }
  33. else{ //this is worker, wait for sync thread finish and set_go()
  34. wait_go()
  35. ....................
  36. }
  37. }
  38.  
  39. beging waiting ...
  40. beging waiting ...
  41. beging waiting ...
  42. beging waiting ...
  43. beging waiting ...
  44. wait done
  45. outside of while waiting !!!
  46. outside of while waiting !!!
  47. outside of while waiting !!!
  48. outside of while waiting !!!
  49. outside of while waiting !!!
  50. outside of while waiting !!!
  51. outside of while waiting !!!
  52. outside of while waiting !!!
  53. outside of while waiting !!!
  54. outside of while waiting !!!
  55. outside of while waiting !!!
  56. outside of while waiting !!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement