Guest User

Untitled

a guest
Jan 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /*
  2. * pthread.cpp
  3. *
  4. * Created on: Aug 26, 2011
  5. * Author: jhkim
  6. */
  7.  
  8. #include <iostream>
  9.  
  10. #include <pthread.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <vector>
  14. using namespace std;
  15.  
  16. void *t1(void*);
  17. void *t2(void*);
  18. void wait_sec(int _t);
  19.  
  20. pthread_mutex_t *sync_mutex_t1;
  21. pthread_mutex_t *sync_mutex_t2;
  22. pthread_cond_t *sync_cond_t1;
  23. pthread_cond_t *sync_cond_t2;
  24.  
  25. int main() {
  26.  
  27. vector<void *(*)(void *)> thread_list;
  28. vector<pthread_t> tident(10);
  29.  
  30. int threasult;
  31. int status;
  32. int i;
  33.  
  34. sync_mutex_t1 = new pthread_mutex_t;
  35. sync_mutex_t2 = new pthread_mutex_t;
  36.  
  37. sync_cond_t1 = new pthread_cond_t;
  38. sync_cond_t2 = new pthread_cond_t;
  39.  
  40. pthread_mutex_init(sync_mutex_t1, NULL);
  41. pthread_mutex_init(sync_mutex_t2, NULL);
  42.  
  43. pthread_cond_init(sync_cond_t1, NULL);
  44. pthread_cond_init(sync_cond_t2, NULL);
  45.  
  46. thread_list.push_back(t1);
  47. thread_list.push_back(t2);
  48.  
  49. #if 1
  50. // create thread
  51. for( i=0; i < thread_list.size(); i++ )
  52. {
  53. if( pthread_create(&tident[i], NULL, thread_list[i], (void *)NULL) < 0 )
  54. exit(0);
  55. }
  56.  
  57. // wait 5 second..
  58. cout<<__FUNCTION__<< " wait .. " <<endl;
  59. wait_sec(5);
  60.  
  61. // send signal to t1 thread
  62. cout<<__FUNCTION__<< " signal!! .." <<endl;
  63. pthread_mutex_lock(sync_mutex_t1);
  64. pthread_cond_signal(sync_cond_t1);
  65. pthread_mutex_unlock(sync_mutex_t1);
  66.  
  67. // let's join!!
  68. for(i=0; i<tident.size(); i++)
  69. {
  70. pthread_join(tident[i], (void **)&status);
  71. }
  72. cout<<__FUNCTION__<< " finish!! .." <<endl;
  73. #endif
  74.  
  75. return 0;
  76. }
  77.  
  78. #if 1
  79. void *t1(void*)
  80. {
  81. cout<<__FUNCTION__<< " Start! " <<endl;
  82.  
  83. // wait until get _cond_signal (cond_t1)
  84. pthread_mutex_lock(sync_mutex_t1);
  85. pthread_cond_wait(sync_cond_t1, sync_mutex_t1);
  86. pthread_mutex_unlock(sync_mutex_t1);
  87.  
  88. // wait 5 sec ..
  89. wait_sec(5);
  90.  
  91. // send signal to t2 thread
  92. pthread_mutex_lock(sync_mutex_t2);
  93. pthread_cond_signal(sync_cond_t2);
  94. pthread_mutex_unlock(sync_mutex_t2);
  95.  
  96.  
  97. cout<<__FUNCTION__<< " Finish! " <<endl;
  98. }
  99.  
  100.  
  101. void *t2(void*)
  102. {
  103. cout<<__FUNCTION__<< " Start! " <<endl;
  104.  
  105. // wait until get _cond_signal (cond_t1)
  106. pthread_mutex_lock(sync_mutex_t2);
  107. pthread_cond_wait(sync_cond_t2, sync_mutex_t2);
  108. pthread_mutex_unlock(sync_mutex_t2);
  109.  
  110. // wait 5 sec ..
  111. wait_sec(5);
  112.  
  113. cout<<__FUNCTION__<< " Finish! " <<endl;
  114. }
  115.  
  116. void wait_sec(int _t)
  117. {
  118. for(int i=0; i<_t; i++)
  119. {
  120. cout<< "." <<endl;
  121. sleep(1);
  122. }
  123. }
  124. #endif
Add Comment
Please, Sign In to add comment