Linux多线程──主线程和子线程分别循环一定次数


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

要注意条件变量的自动复位问题。参看这篇文章:Linux 的多线程编程的高效开发经验

代码:

  1. #include <pthread.h>   
  2. #include <stdio.h>   
  3.   
  4. // 互斥锁,条件变量   
  5. pthread_mutex_t mutex;  
  6. pthread_cond_t cond;  
  7.   
  8. // 循环次数   
  9. int main_count = 0;  
  10. int subthread_count = 0;  
  11.   
  12. // 线程等待标志   
  13. bool main_thread_wait_flag = false;  
  14. bool subthread_wait_flag = false;  
  15.   
  16. void main_thread_func();  
  17. void *subthread_func(void *arg);  
  18.   
  19. int main(int argc, char **argv)  
  20. {  
  21.     pthread_t tid;  
  22.   
  23.     pthread_mutex_init(&mutex, NULL);  
  24.     pthread_cond_init(&cond, NULL);  
  25.       
  26.     pthread_create(&tid, NULL, subthread_func, NULL);  
  27.     main_thread_func();  
  28.     pthread_join(tid, NULL);  
  29.       
  30.     return 0;  
  31. }  
  32.   
  33. void main_thread_func()  
  34. {  
  35.     while (true)  
  36.     {  
  37.         pthread_mutex_lock(&mutex);  
  38.         main_thread_wait_flag = true;  
  39.         pthread_cond_wait(&cond, &mutex);  
  40.         main_thread_wait_flag = false;  
  41.         pthread_mutex_unlock(&mutex);  
  42.           
  43.         for (int i = 1; i <= 100; ++i)  
  44.         {  
  45.             fprintf(stdout, "main thread: %d\n", i);  
  46.         }  
  47.           
  48.           
  49.         while (true)  
  50.         {  
  51.             pthread_mutex_lock(&mutex);  
  52.             if (true == subthread_wait_flag)  
  53.             {  
  54.                 pthread_cond_signal(&cond);  
  55.                 pthread_mutex_unlock(&mutex);  
  56.                 break;  
  57.             }  
  58.             pthread_mutex_unlock(&mutex);  
  59.         }  
  60.           
  61.         ++main_count;  
  62.         if (main_count >= 50)  
  63.         {  
  64.             fprintf(stdout, "main thread loop 50 times\n");  
  65.             break;  
  66.         }  
  67.     }  
  68. }  
  69.   
  70. void *subthread_func(void *arg)  
  71. {  
  72.     while (true)  
  73.     {  
  74.         for (int i = 1; i <= 10; ++i)  
  75.         {  
  76.             fprintf(stdout, "subthread: %d\n", i);  
  77.         }  
  78.   
  79.         while (true)  
  80.         {  
  81.             pthread_mutex_lock(&mutex);  
  82.             if (true == main_thread_wait_flag)  
  83.             {  
  84.                 pthread_cond_signal(&cond);  
  85.                 pthread_mutex_unlock(&mutex);  
  86.                 break;  
  87.             }  
  88.             pthread_mutex_unlock(&mutex);  
  89.         }  
  90.         pthread_mutex_lock(&mutex);  
  91.         subthread_wait_flag = true;  
  92.         pthread_cond_wait(&cond, &mutex);  
  93.         subthread_wait_flag = false;  
  94.         pthread_mutex_unlock(&mutex);  
  95.           
  96.         ++subthread_count;  
  97.         if (subthread_count >= 50)  
  98.         {  
  99.             fprintf(stdout, "subthread loop 50 times\n");  
  100.             break;  
  101.         }  
  102.     }  
  103.     return (void *)0;  
  104. }  

相关内容