两个线程交替打印信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两个线程交替打印信息相关的知识,希望对你有一定的参考价值。

看见一个关于两个线程交替打印信息的题目,题目大概是

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

 

写了两个版本,一个是用了mutex,不用条件变量;另外一个是用条件变量。

第一个,不用条件变量

技术分享
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <pthread.h>
 4 
 5 
 6 
 7 const int LOOP_2 = 2;
 8 const int LOOP_5 = 5;
 9 int loop_count_slave = 10;
10 int loop_count_main = 10;
11 
12 enum bool { FALSE = 0,TRUE =1};
13 enum bool slave_can_print = TRUE;
14 enum bool main_can_print = FALSE;
15 
16 pthread_cond_t cond_slave = PTHREAD_COND_INITIALIZER;
17 pthread_cond_t cond_main =  PTHREAD_COND_INITIALIZER;
18 
19 
20 pthread_mutex_t mutex_slave = PTHREAD_MUTEX_INITIALIZER;
21 pthread_mutex_t mutex_main = PTHREAD_MUTEX_INITIALIZER;
22 
23 void* thread_fun(void* arg);
24 void printf_msg(int type);
25 
26 int main(int argc, char* argv[])
27 {
28     pthread_t tid;
29     int ret;
30     ret = pthread_create(&tid,NULL,thread_fun,NULL);
31     if( ret != 0 )
32     {
33         strerror(ret);
34     }
35     while(loop_count_main > 0)
36     {
37         pthread_mutex_lock(&mutex_main);
38         if(main_can_print)
39         {
40             printf_msg(1);
41             loop_count_main -- ;
42             pthread_mutex_lock(&mutex_slave);
43             slave_can_print = TRUE;
44             pthread_mutex_unlock(&mutex_slave);
45             main_can_print = FALSE;
46         }
47         pthread_mutex_unlock(&mutex_main);
48     }
49 
50 
51 }
52 void* thread_fun(void* arg)
53 {
54     while(loop_count_slave > 0)
55     {
56         pthread_mutex_lock(&mutex_slave);
57         if(slave_can_print)
58         {
59             printf_msg(2);
60             loop_count_slave -- ;
61             slave_can_print = FALSE;
62             pthread_mutex_lock(&mutex_main);
63             main_can_print = TRUE;
64             pthread_mutex_unlock(&mutex_main);
65         }
66         pthread_mutex_unlock(&mutex_slave);
67     }
68     return NULL;
69 }
70 void printf_msg(int type)
71 {
72     if(type == 1)
73     {
74         int i = 0;
75         for(; i < LOOP_5; i++)
76         {
77             printf("from master \n");
78 
79         }
80     }
81     else if(type == 2)
82     {
83         int i = 0;
84         for(; i < LOOP_2; i++)
85         {
86             printf("from slave thread\n");
87         }
88     }
89 }
View Code

第二个,使用条件变量

技术分享
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <pthread.h>
 4 
 5 
 6 
 7 const int LOOP_2 = 2;
 8 const int LOOP_5 = 5;
 9 int loop_count_slave = 10;
10 int loop_count_main = 10;
11 
12 enum bool { FALSE = 0,TRUE =1};
13 enum bool slave_can_print = TRUE;
14 enum bool main_can_print = FALSE;
15 
16 pthread_cond_t cond_slave = PTHREAD_COND_INITIALIZER;
17 pthread_cond_t cond_main =  PTHREAD_COND_INITIALIZER;
18 
19 
20 pthread_mutex_t mutex_slave = PTHREAD_MUTEX_INITIALIZER;
21 pthread_mutex_t mutex_main = PTHREAD_MUTEX_INITIALIZER;
22 
23 void* thread_fun(void* arg);
24 void printf_msg(int type);
25 
26 int main(int argc, char* argv[])
27 {
28     pthread_t tid;
29     int ret;
30     ret = pthread_create(&tid,NULL,thread_fun,NULL);
31     if( ret != 0 )
32     {
33         strerror(ret);
34     }
35     while(loop_count_main > 0)
36     {
37         pthread_mutex_lock(&mutex_main);
38         if(main_can_print)
39         {
40             printf_msg(1);
41             loop_count_main -- ;
42             main_can_print = FALSE ;
43             pthread_mutex_lock(&mutex_slave);
44             slave_can_print = TRUE;
45             pthread_mutex_unlock(&mutex_slave);
46             pthread_cond_signal(&cond_slave);
47         }
48         else
49         {
50             pthread_cond_wait(&cond_main,&mutex_main);
51         }
52         pthread_mutex_unlock(&mutex_main);
53     }
54 
55 }
56 void* thread_fun(void* arg)
57 {
58     while(loop_count_slave > 0)
59     {
60         pthread_mutex_lock(&mutex_slave);
61         if(slave_can_print)
62         {
63             printf_msg(2);
64             loop_count_slave -- ;
65             slave_can_print = FALSE;
66             pthread_mutex_lock(&mutex_main);
67             main_can_print = TRUE;
68             pthread_mutex_unlock(&mutex_main);
69             pthread_cond_signal(&cond_main);
70         }
71         else
72         {
73             pthread_cond_wait(&cond_slave,&mutex_slave);
74         }
75         pthread_mutex_unlock(&mutex_slave);
76     }
77 
78     return NULL;
79 }
80 void printf_msg(int type)
81 {
82     if(type == 1)
83     {
84         int i = 0;
85         for(; i < LOOP_5; i++)
86         {
87             printf("from master \n");
88 
89         }
90     }
91     else if(type == 2)
92     {
93         int i = 0;
94         for(; i < LOOP_2; i++)
95         {
96             printf("from slave thread\n");
97         }
98     }
99 }
View Code

 

比较两次执行,使用条件变量大概耗时只有0.002s,不使用条件变量大概0.2s,性能相差巨大。

原因在于不使用条件变量时,一直在忙循环,测试条件。

以上是关于两个线程交替打印信息的主要内容,如果未能解决你的问题,请参考以下文章

经典面试题——两个线程交替打印奇数和偶数

面试题:用程序实现两个线程交替打印 0~100 的奇偶数

两个线程交替打印1-99

实现两个线程从0-100交替打印

两个线程交替打印奇数和偶数

两个线程交替打印