C多线程练习题

来源:互联网 发布:淘宝客app免费制作 编辑:程序博客网 时间:2024/06/05 17:48
题目:

子线程循环 3 次,接着主线程循环 6 次,接着又回到子线程循环 3 次,接着再回到主线程又循环次,如此循环50次,试写出代码。

程序:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <pthread.h>  
  4.   
  5. pthread_mutex_t mutex;  
  6. pthread_cond_t cond;  
  7.   
  8. int main_flog = 0;  
  9. int sub_flog = 0;  
  10.   
  11. int main_count = 0;  
  12. int sub_count = 0;  
  13.   
  14. void *sub_thread_func(void *argv);  
  15. void main_thread_func();  
  16.   
  17. int main()  
  18. {  
  19.     pthread_t tid;  
  20.     int ret;  
  21.     pthread_mutex_init(&mutex,NULL);  
  22.     pthread_cond_init(&cond,NULL);  
  23.   
  24.     ret = pthread_create(&tid,NULL,sub_thread_func,NULL);  
  25.     if(ret == -1)  
  26.     {  
  27.         perror("pthread_create errpr!\n");  
  28.         exit(-1);  
  29.     }  
  30.   
  31.     main_thread_func();  
  32.     ret = pthread_join(tid,NULL);  
  33.     if(ret == -1)  
  34.     {  
  35.         printf("pthread_join error!\n");  
  36.         exit(-1);  
  37.     }  
  38.     return 0;  
  39. }  
  40. void *sub_thread_func(void *argv)  
  41. {  
  42.     int i;  
  43.   
  44.     while(1)  
  45.     {  
  46.         sub_count++;  
  47.         for(i = 1; i <= 3; i++)  
  48.         {  
  49.             printf("sub pthread%d:%d\n",sub_count,i);  
  50.         }  
  51.         while(1)  
  52.         {  
  53.             pthread_mutex_lock(&mutex);  
  54.             if(main_flog == 1)  
  55.             {  
  56.                 pthread_cond_signal(&cond);  
  57.                 pthread_mutex_unlock(&mutex);  
  58.                 break;  
  59.             }  
  60.             pthread_mutex_unlock(&mutex);  
  61.         }  
  62.         pthread_mutex_lock(&mutex);  
  63.         sub_flog = 1;  
  64.         pthread_cond_wait(&cond,&mutex);  
  65.         sub_flog = 0;  
  66.         pthread_mutex_unlock(&mutex);  
  67.   
  68.         //sub_count++;  
  69.         if(sub_count >= 50)  
  70.         {  
  71.             printf("sub_pthread loop 50 times!\n");  
  72.             break;  
  73.         }  
  74.     }  
  75.     return (void *)0;  
  76. }  
  77. void main_thread_func(void)  
  78. {  
  79.     int i;  
  80.   
  81.     while(1)  
  82.     {  
  83.         main_count++;  
  84.         pthread_mutex_lock(&mutex);  
  85.         main_flog = 1;  
  86.         pthread_cond_wait(&cond,&mutex);  
  87.         main_flog = 0;  
  88.         pthread_mutex_unlock(&mutex);  
  89.   
  90.         for(i = 1; i <= 6; i++)  
  91.         {  
  92.             printf("main    phread%d:%d\n",main_count,i);  
  93.         }  
  94.         while(1)  
  95.         {  
  96.             pthread_mutex_lock(&mutex);  
  97.             if(sub_flog == 1)  
  98.             {  
  99.                 pthread_cond_signal(&cond);  
  100.                 pthread_mutex_unlock(&mutex);  
  101.                 break;  
  102.             }  
  103.             pthread_mutex_unlock(&mutex);  
  104.         }  
  105.         //main_count++;  
  106.         if(main_count >= 50)  
  107.         {  
  108.             printf("main_phread loop 50 times!\n");  
  109.             break;  
  110.         }  
  111.     }  
  112. }  

0 0
原创粉丝点击