本文主要是介绍编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
好长时间没有再想写代码了,虽然自己还没有毕业,处于学习的阶段,但是很少是主动的写过多少,代码量太少是一大障碍。在不久就面临毕业找工作,刚才看了许久的各大公司的笔试题,看懂的没有几道,会的更少。自己弱爆了,怎么办。在这里,我把自己会的尽量写,借此激励自己。
老师讲的这是迅雷的笔试题: 是不是无关紧要,主要是上次考试的时候这就是最后一道题,要求用手写确实不容易.
题意很明了,要求也很简单,自己傻逼一样一看就笑了,用一个sleep()愉快的完成了这道题,感觉还很牛B..下面是当时自己在试卷上的作答:
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<errno.h>
- #include<pthread.h>
- #include<unistd.h>
- void *th_fun(void* arg){
- int i=0;
- while(i < 10){
- printf("the num is:%lx\n",pthread_self());
- i++;
- usleep(150);
- }
- return ((void *)0);
- }
- int main(void){
- pthread_t th1,th2,th3;
- //参数的类型: int*
- if(0 != pthread_create(&th1,NULL,th_fun,NULL)){
- fprintf(stderr,"pthread %s\n",strerror(errno));
- exit(1);
- }
- usleep(50);
- if(0 != pthread_create(&th2,NULL,th_fun,NULL)){
- fprintf(stderr,"pthread %s\n",strerror(errno));
- exit(1);
- }
- usleep(50);
- if(0 != pthread_create(&th3,NULL,th_fun,NULL)){
- fprintf(stderr,"pthread %s\n",strerror(errno));
- exit(1);
- }
- pthread_join(th1,NULL);
- pthread_join(th2,NULL);
- pthread_join(th3,NULL);
- return 0;
- }
- </unistd></pthread></errno></string></stdlib></stdio>
假设有三个线程A, B, C;A线程首先执行,B线程等待A结束后执行,C线程等待B结束后执行,A线程又要等待C线程结束后执行,B再等待A,C再等待B....以此类推,是一个循环等待的过程,每一个线程要做的事情:1.抢到锁,设置标志位为1(告诉上家自己在等待信号);2.收到上家的信号,立即设置标志为为0,并打印出自己的线程号;3.确定下家是否在等待,确定下家在等待后,立即发送信号给下家,再打开锁,usleep(20)(让其他线程抢到锁)。
其中有两个要注意的地方:1.三个线程创建好后,三个线程都在死等待 [A等C,B等A,C等B] 所以首先手动代替C为A发送信号,让程序执行。2.A线程的10次循环结束后,flag标志位是0,而此时C线程还没有执行完毕,C要检查A的标志为是否为1再给A发送信号,所以在A循环结束后把它的flag标志为置为1。
看程序:
- #include<stdio.h>
- #include<errno.h>
- #include<string.h>
- #include<stdlib.h>
- #include<pthread.h>
- #define PRINTTIME 10
- /*互斥锁 MUTEX*/
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- /*条件变量*/
- pthread_cond_t condA_B = PTHREAD_COND_INITIALIZER;
- pthread_cond_t condB_C = PTHREAD_COND_INITIALIZER;
- pthread_cond_t condC_A = PTHREAD_COND_INITIALIZER;
- int flagA = 0,flagB = 0,flagC = 0;
- void *th_A_C(void *arg){
- printf("A come!\n");
- int i = 0;
- char *name = (char *)arg;
- while(i < PRINTTIME){
- pthread_mutex_lock(&mutex);
- //printf("A LOCKED\n");
- flagA = 1;
- /*等待C的信号*/
- pthread_cond_wait(&condC_A,&mutex);
- flagA = 0;
- printf("%c:%d-->%lx\n",*name,i,pthread_self());
- /*发信号给B*/
- /*在发信号给B之前,先检测B有没有在等待*/
- /*若B在等待,在这里就可以检测到标志位flagB == 1*/
- while(!flagB){
- pthread_mutex_unlock(&mutex);
- usleep(30);
- // printf("--------------------------wait flagB\n");
- pthread_mutex_lock(&mutex);
- }/*循环结束表示flagBC为1,就是B已经加锁,并在等待A给它信号,所以现在A发信号给B*/
- // printf("signal A--->B finished!\n");
- pthread_cond_signal(&condA_B);
- pthread_mutex_unlock(&mutex);
- // printf("%c UNLOCK!\n",*name);
- i++;
- usleep(20);
- }
- /*A的循环结束的时候把自己的flagA 位置为1,欺骗C线程 让C线程以为A这边准备好了,然后发信号给他。这样可以避免C判断A没有再等带它的标志一直死循环状态退不出来*/
- flagA = 1;
- }
- void *th_B_A(void *arg){
- printf("B come!\n");
- int i = 0;
- char *name = (char *)arg;
- while(i < PRINTTIME){
- pthread_mutex_lock(&mutex);
- // printf("B LOCKED\n");
- /*B抢到锁,设置标志位flagBC = 1*/
- flagB = 1;
- /*等待A的信号*/
- pthread_cond_wait(&condA_B,&mutex);
- flagB = 0;
- printf("%c:%d-->%lx\n",*name,i,pthread_self());
- /*发信号给C*/
- /*在发信号给C之前先检测C有没有在等待*/
- /*若C在等待则flagC == 1*/
- while(!flagC){
- pthread_mutex_unlock(&mutex);
- usleep(30);
- // printf("--------------------------wait flagC\n");
- pthread_mutex_lock(&mutex);
- }
- /*循环结束表示flagC 已经为1 ,所以开始给C发信号*/
- // printf("signal B--->C finished!\n");
- pthread_cond_signal(&condB_C);
- pthread_mutex_unlock(&mutex);
- // printf("%c UNLOCK!\n",*name);
- i++;
- usleep(20);
- }
- }
- void *th_C_B(void *arg){
- printf("C come!\n");
- int i = 0;
- char *name = (char *)arg;
- while(i < PRINTTIME){
- pthread_mutex_lock(&mutex);
- // printf("C LOCKED\n");
- flagC = 1;
- /*等待B的信号*/
- pthread_cond_wait(&condB_C,&mutex);
- flagC = 0;
- printf("%c:%d-->%lx\n",*name,i,pthread_self());
- /*发信号给A*/
- /*发信号给A 之前先检测A有没有在等待,若在等待 则flagA == 1*/
- while(!flagA){
- pthread_mutex_unlock(&mutex);
- usleep(30);
- // printf("--------------------------wait flagA\n");
- pthread_mutex_lock(&mutex);
- }
- /*循环结束表示A在等待,所以发信号给A*/
- // printf("signal C--->A finished!\n");
- pthread_cond_signal(&condC_A);
- pthread_mutex_unlock(&mutex);
- // printf("%c UNLOCK!\n",*name);
- i++;
- usleep(20);
- }
- }
- int main(void){
- pthread_t th1,th2,th3;
- char name1 = 'A',name2 = 'B',name3 = 'C';
- int *p1 =(int *) &name1;
- int *p2 =(int *) &name2;
- int *p3 =(int *) &name3;
- if(pthread_create(&th1,NULL,th_A_C,(void *)p1) < 0){
- fprintf(stderr,"pthread_create th1 %s\n",strerror(errno));
- exit(1);
- }
- usleep(30);
- if(pthread_create(&th2,NULL,th_B_A,(void *)p2) < 0){
- fprintf(stderr,"pthread_create th2 %s\n",strerror(errno));
- exit(1);
- }
- usleep(30);
- if(pthread_create(&th3,NULL,th_C_B,(void *)p3) < 0){
- fprintf(stderr,"pthread_create th1 %s\n",strerror(errno));
- exit(1);
- }
- //起始给线程A发通知,防止A,b,c,死锁
- /*直接通知A,不能内个让他再傻等*/
- pthread_cond_broadcast(&condC_A);
- pthread_join(th1,NULL);
- printf("th1 finished!\n");
- pthread_join(th2,NULL);
- printf("th2 finished!\n");
- pthread_join(th3,NULL);
- printf("th3 finished!\n");
- return 0;
- }
- </pthread></stdlib></string></errno></stdio>
程序顺利执行,结果正确。但是代码冗余很大,可扩展性不好,在下面做改进。。
改进版的, 把A,B,C先程中的大量重复部分封装成一个函数,用循环创建和回收线程。
- #include<stdio.h>
- #include<errno.h>
- #include<string.h>
- #include<stdlib.h>
- #include<pthread.h>
- #define PRINTTIME 10
- /*互斥锁 MUTEX*/
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- /*条件变量*/
- pthread_cond_t condA_B = PTHREAD_COND_INITIALIZER;
- pthread_cond_t condB_C = PTHREAD_COND_INITIALIZER;
- pthread_cond_t condC_A = PTHREAD_COND_INITIALIZER;
- int flagA = 0,flagB = 0,flagC = 0;
- void pthread_print(pthread_cond_t *cond_wait,char *name,int *flag_self,int *flag_wait,pthread_cond_t *cond_signal){
- printf("%c come!\n",*name);
- int i = 0;
- while(i < PRINTTIME){
- pthread_mutex_lock(&mutex);
- *flag_self = 1;
- pthread_cond_wait(cond_wait,&mutex);
- *flag_self = 0;
- printf("%c:%d-->%lx\n",*name,i,pthread_self());
- while(!(*flag_wait)){
- pthread_mutex_unlock(&mutex);
- usleep(30);
- pthread_mutex_lock(&mutex);
- }/*循环结束表示flagBC为1,就是B已经加锁,并在等待A给它信号,所以现在A发信号给B*/
- pthread_cond_signal(cond_signal);
- pthread_mutex_unlock(&mutex);
- i++;
- usleep(20);
- }
- }
- void *th_A_C(void *arg){
- char *name = (char *)arg;
- pthread_print(&condC_A,name,&flagA,&flagB,&condA_B);
- flagA = 1;/*防止C 等待 A 而不能结束*/
- }
- void *th_B_A(void *arg){
- char *name = (char *)arg;
- pthread_print(&condA_B,name,&flagB,&flagC,&condB_C);
- }
- void *th_C_B(void *arg){
- char *name = (char *)arg;
- pthread_print(&condB_C,name,&flagC,&flagA,&condC_A);
- }
- int main(void){
- char name1 = 'A',name2 = 'B',name3 = 'C';
- char *p[] = {
- (char *) &name1,
- (char *) &name2,
- (char *) &name3
- };
- void *(*th_funs[])(void *) = {
- th_A_C,
- th_B_A,
- th_C_B
- };
- int count = sizeof(th_funs)/sizeof(th_funs[0]);
- pthread_t th[count];
- int i = 0;
- while(i < count){
- if(pthread_create(&th[i],NULL,th_funs[i],(void *)p[i]) < 0){
- fprintf(stderr,"pthread_create th1 %s\n",strerror(errno));
- exit(1);
- }
- usleep(30);
- i++;
- }
- pthread_cond_broadcast(&condC_A);
- for(i = 0;i < count;i++){
- pthread_join(th[i],NULL);
- printf("th%d finished!\n",i);
- }
- return 0;
- }
- </pthread></stdlib></string></errno></stdio>
上一个封装的不好,最后一个版本老师给的,封装的更好一些:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <pthread.h>
- #define LP_TIMES 10
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t cond_AB = PTHREAD_COND_INITIALIZER;
- pthread_cond_t cond_BC = PTHREAD_COND_INITIALIZER;
- pthread_cond_t cond_CA = PTHREAD_COND_INITIALIZER;
- //标记
- int flag_AB,flag_BC,flag_CA;
- //标记检查的线程等待
- void th_condflag_wait(int *flag,pthread_cond_t *cond,pthread_mutex_t *mutex)
- {
- (*flag) = 1;
- pthread_cond_wait(cond,mutex);
- (*flag) = 0;
- }
- //标记检查的线程通知
- void th_condflag_broadcast(int *flag,pthread_cond_t *cond,pthread_mutex_t *mutex)
- {
- while(!(*flag))
- {
- pthread_mutex_unlock(mutex);
- usleep(50);
- pthread_mutex_lock(mutex);
- }
- pthread_cond_broadcast(cond);
- }
- void *th_fun_A(void *arg)
- {
- int i = LP_TIMES;
- while(i--)
- {
- pthread_mutex_lock(&mutex);
- //A wait C
- th_condflag_wait(&flag_CA,&cond_CA,&mutex);
- printf("A%d: %lx\n",10-i,pthread_self());
- //A cond B
- th_condflag_broadcast(&flag_AB,&cond_AB,&mutex);
- pthread_mutex_unlock(&mutex);
- usleep(50);
- }
- //防止C线程最后一次等待A线程时死锁
- flag_CA = 1;
- }
- void *th_fun_B(void *arg)
- {
- int i = LP_TIMES;
- while(i--)
- {
- pthread_mutex_lock(&mutex);
- //B wait A
- th_condflag_wait(&flag_AB,&cond_AB,&mutex);
- printf("B%d: %lx\n",10-i,pthread_self());
- //B cond C
- th_condflag_broadcast(&flag_BC,&cond_BC,&mutex);
- pthread_mutex_unlock(&mutex);
- usleep(50);
- }
- }
- void *th_fun_C(void *arg)
- {
- int i = LP_TIMES;
- while(i--)
- {
- pthread_mutex_lock(&mutex);
- //C wait B
- th_condflag_wait(&flag_BC,&cond_BC,&mutex);
- printf("C%d: %lx\n",10-i,pthread_self());
- //C cond A
- th_condflag_broadcast(&flag_CA,&cond_CA,&mutex);
- pthread_mutex_unlock(&mutex);
- usleep(50);
- }
- }
- int main(void)
- {
- printf("main: %lx\n",pthread_self());
- //保存3个线程的处理函数
- void *(*th_funs[])(void *) =
- {
- th_fun_A,
- th_fun_B,
- th_fun_C
- };
- int th_count = sizeof(th_funs)/sizeof(th_funs[0]);
- pthread_t th[th_count];
- int i;
- for(i = 0;i < th_count;i++)
- {
- //通过线程函数数组记录的函数来创建线程
- if(pthread_create(th+i,NULL,th_funs[i],(void *)i) < 0)
- {
- fprintf(stderr,"th_create: %s\n",
- strerror(errno));
- exit(1);
- }
- printf("th[%d]: %lx\n",i,th[i]);
- }
- //起始给A线程发出通知,防止A和C死锁
- pthread_mutex_lock(&mutex);
- th_condflag_broadcast(&flag_CA,&cond_CA,&mutex);
- pthread_mutex_unlock(&mutex);
- //回收线程
- for(i = 0;i < th_count;i++)
- {
- pthread_join(th[i],NULL);
- printf("i: %d finished!\n",i);
- }
- return 0;
- }
- </pthread></errno></string></stdlib></stdio>
这篇关于编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!