线程编程(3)

来源:互联网 发布:安卓手游 知乎 编辑:程序博客网 时间:2024/05/18 20:47


有四个线程1234。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:

A1 2 3 4 1 2....

B2 3 4 1 2 3....

C3 4 1 2 3 4....

D4 1 2 3 4 1....

请设计程序。


#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <errno.h>FILE *f1;FILE *f2;FILE *f3;FILE *f4;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_D = PTHREAD_COND_INITIALIZER;pthread_cond_t condD_A = PTHREAD_COND_INITIALIZER;int flaga,flagb,flagc,flagd;void *th_fun_a(void *arg){int i = 0;while(i < 10){pthread_mutex_lock(&mutex);flaga = 1;pthread_cond_wait(&condD_A,&mutex);flaga = 0;f1 = fopen("A.txt","a+");//fseek(f1,0,SEEK_END);fputc('1',f1);fclose(f1);f2 = fopen("B.txt","a+");//fseek(f2,0,SEEK_END);fputc('2',f2);fclose(f2);f3 = fopen("C.txt","a+");//fseek(f3,0,SEEK_END);fputc('3',f3);fclose(f3);f4 = fopen("D.txt","a+");//fseek(f4,0,SEEK_END);fputc('4',f4);fclose(f4);while(!flagb){pthread_mutex_unlock(&mutex);usleep(50);pthread_mutex_lock(&mutex);}pthread_cond_broadcast(&condA_B);pthread_mutex_unlock(&mutex);i++;usleep(50);}flaga = 1;}void *th_fun_b(void *arg){int i = 0;while(i < 10){pthread_mutex_lock(&mutex);flagb = 1;pthread_cond_wait(&condA_B,&mutex);flagb = 0;f1 = fopen("A.txt","a+");//fseek(f1,0,SEEK_END);fputc('2',f1);fclose(f1);f2 = fopen("B.txt","a+");//fseek(f2,0,SEEK_END);fputc('3',f2);fclose(f2);f3 = fopen("C.txt","a+");//fseek(f3,0,SEEK_END);fputc('4',f3);fclose(f3);f4 = fopen("D.txt","a+");//fseek(f4,0,SEEK_END);fputc('1',f4);fclose(f4);while(!flagc){pthread_mutex_unlock(&mutex);usleep(50);pthread_mutex_lock(&mutex);}pthread_cond_broadcast(&condB_C);pthread_mutex_unlock(&mutex);i++;usleep(50);}}void *th_fun_c(void *arg){int i = 0;while(i < 10){pthread_mutex_lock(&mutex);flagc = 1;pthread_cond_wait(&condB_C,&mutex);flagc = 0;f1 = fopen("A.txt","a+");//fseek(f1,0,SEEK_END);fputc('3',f1);fclose(f1);f2 = fopen("B.txt","a+");//fseek(f2,0,SEEK_END);fputc('4',f2);fclose(f2);f3 = fopen("C.txt","a+");fseek(f3,0,SEEK_END);fputc('1',f3);fclose(f3);f4 = fopen("D.txt","a+");//fseek(f4,0,SEEK_END);fputc('2',f4);fclose(f4);while(!flagd){pthread_mutex_unlock(&mutex);usleep(50);pthread_mutex_lock(&mutex);}pthread_cond_broadcast(&condC_D);pthread_mutex_unlock(&mutex);i++;usleep(50);}}void *th_fun_d(void *arg){int i = 0;while(i < 10){pthread_mutex_lock(&mutex);flagd = 1;pthread_cond_wait(&condC_D,&mutex);flagd = 0;f1 = fopen("A.txt","a+");//fseek(f1,0,SEEK_END);fputc('4',f1);fclose(f1);f2 = fopen("B.txt","a+");//fseek(f2,0,SEEK_END);fputc('1',f2);fclose(f2);f3 = fopen("C.txt","a+");//fseek(f3,0,SEEK_END);fputc('2',f3);fclose(f3);f4 = fopen("D.txt","a+");//fseek(f4,0,SEEK_END);fputc('3',f4);fclose(f4);while(!flaga){pthread_mutex_unlock(&mutex);usleep(50);pthread_mutex_lock(&mutex);}pthread_cond_broadcast(&condD_A);pthread_mutex_unlock(&mutex);i++;usleep(50);}}int main(){pthread_t th[4];void *(*th_funs[])(void *) = {th_fun_a,th_fun_b,th_fun_c,th_fun_d,};int i;for(i = 0; i < 4; i++){if(pthread_create(th+i,NULL,th_funs[i],(void *)i) < 0){fprintf(stderr,"creat:%s\n",strerror(errno));exit(-1);}}pthread_mutex_lock(&mutex);while(!flagd){pthread_mutex_unlock(&mutex);usleep(50);pthread_mutex_lock(&mutex);}printf("hi\n");pthread_cond_broadcast(&condD_A);pthread_mutex_unlock(&mutex);for(i = 0; i < 4; i++){pthread_join(th[i],NULL);printf("process %d finished!\n",i);}    return 0;}

0 0
原创粉丝点击