Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. #include <iomanip>
  5.  
  6. #include <iostream>
  7.  
  8. #include <pthread.h>
  9.  
  10. #include <unistd.h>
  11.  
  12. using namespace std;
  13.  
  14. void* func1(void* a){
  15.  
  16. bool*flag1=(bool*)a;
  17.  
  18. while(*flag1 ){
  19.  
  20. cout<< "1"<< flush;
  21.  
  22. sleep(1);
  23.  
  24. }
  25.  
  26. pthread_exit((void*)3);
  27.  
  28. }
  29.  
  30. void* func2(void* a){
  31.  
  32. bool*flag2=(bool*)a;
  33.  
  34. while(*flag2){
  35.  
  36. cout<< "2"<< flush;
  37.  
  38. sleep(1);
  39.  
  40. }
  41.  
  42. pthread_exit((void*)4);
  43.  
  44. }
  45.  
  46. int main(){
  47.  
  48. bool flag1=true;
  49.  
  50. bool flag2=true;
  51.  
  52. pthread_t thread1,thread2;
  53.  
  54. pthread_create(&thread1, NULL, func1, &flag1);
  55.  
  56. pthread_create(&thread2, NULL, func2, &flag2);
  57.  
  58. getchar();
  59.  
  60. flag1=false;
  61.  
  62. flag2=false;
  63.  
  64. void* st1;
  65.  
  66. void* st2;
  67.  
  68. pthread_join(thread1, &st1);
  69.  
  70. pthread_join(thread2, &st2);
  71.  
  72. cout<<"status 1,2:"<<((int*)st1)<<" "<<((int*)st2);
  73.  
  74. return(0);
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement