Linux多线程──3个子线程轮流运行


迅雷笔试题:

编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

程序:

  1. #include <pthread.h>   
  2. #include <cstdio>   
  3.   
  4. const int THREAD_NUMBER = 3;  
  5.   
  6. // 子线程的互斥量和条件变量   
  7. pthread_mutex_t thread_mutex[THREAD_NUMBER];  
  8. pthread_cond_t thread_cond[THREAD_NUMBER];  
  9.   
  10. // 子线程是否正在等待   
  11. bool thread_wait_flag[THREAD_NUMBER];  
  12.   
  13. // 标识轮到哪个子线程输出其ID   
  14. pthread_mutex_t mutex;  
  15. int thread_turn;  
  16.   
  17. void *thread_func(void *arg);  
  18.   
  19. int main(int argc, char **argv)  
  20. {  
  21.     pthread_t tids[THREAD_NUMBER];  
  22.   
  23.     for (int i = 0; i < THREAD_NUMBER; ++i)  
  24.     {  
  25.         pthread_mutex_init(&thread_mutex[i], NULL);  
  26.         pthread_cond_init(&thread_cond[i], NULL);  
  27.     }  
  28.       
  29.     pthread_mutex_init(&mutex, NULL);  
  30.     thread_turn = 0;  
  31.       
  32.     for (int i = 0; i < THREAD_NUMBER; ++i)  
  33.         thread_wait_flag[i] = false;  
  34.       
  35.     for (int i = 0; i < THREAD_NUMBER; ++i)  
  36.     {  
  37.         pthread_create(&tids[i], NULL, thread_func, (void *)i);  
  38.     }  
  39.   
  40.     for (int i = 0; i < THREAD_NUMBER; ++i)  
  41.     {  
  42.         pthread_join(tids[i], NULL);  
  43.     }  
  44.     printf("\n");  
  45.     return 0;  
  46. }  
  47.   
  48. void *thread_func(void *arg)  
  49. {  
  50.     int id = (int)arg;  
  51.     char ch = 'A' + id;  
  52.     int count = 0;  
  53.       
  54.     while (true)  
  55.     {  
  56.         if (id == thread_turn) // 若轮到当前子线程,输出ID,发送信号   
  57.         {  
  58.             printf("%c", ch);  
  59.             ++count;  
  60.             if (id == THREAD_NUMBER-1 && count == 10) // 若是第3个子线程,输出ID后,可直接退出。   
  61.                 break;  
  62.             pthread_mutex_lock(&mutex);  
  63.             ++thread_turn;  
  64.             thread_turn %= THREAD_NUMBER;  
  65.             pthread_mutex_unlock(&mutex);  
  66.               
  67.             while (true)  
  68.             {  
  69.                 pthread_mutex_lock(&thread_mutex[thread_turn]);  
  70.                 if (true == thread_wait_flag[thread_turn])  
  71.                 {  
  72.                     pthread_cond_signal(&thread_cond[thread_turn]);  
  73.                     pthread_mutex_unlock(&thread_mutex[thread_turn]);  
  74.                     break;  
  75.                 }  
  76.                 pthread_mutex_unlock(&thread_mutex[thread_turn]);  
  77.             }  
  78.             if (count == 10) // 若是第1、2个子线程,发出信号后,退出   
  79.                 break;  
  80.         }  
  81.         else // 否则,等待   
  82.         {  
  83.             pthread_mutex_lock(&thread_mutex[id]);  
  84.             thread_wait_flag[id] = true;  
  85.             pthread_cond_wait(&thread_cond[id], &thread_mutex[id]);  
  86.             thread_wait_flag[id] = false;  
  87.             pthread_mutex_unlock(&thread_mutex[id]);  
  88.         }  
  89.     }  
  90.     return (void *)0;  
  91. }  

相关内容