C语言线程,有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:A:1 2 3 4 1 2
2021/10/7 23:13:57
本文主要是介绍C语言线程,有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:A:1 2 3 4 1 2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#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; }
这篇关于C语言线程,有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:A:1 2 3 4 1 2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28pyqt 怎么打包整个项目-icode9专业技术文章分享
- 2024-09-28laravel Commands 创建带有参数的 Artisan 命令的步骤和示例-icode9专业技术文章分享
- 2024-09-28antd怎么实现渲染tiff图片-icode9专业技术文章分享
- 2024-09-28英文半角中划线和中文全角的中划线有什么区别-icode9专业技术文章分享
- 2024-09-28nvm npm 和node 他们之间有什么关系-icode9专业技术文章分享
- 2024-09-28Node Version Manager (nvm)使用教程-icode9专业技术文章分享
- 2024-09-28nvm命令太慢,是什么原因-icode9专业技术文章分享
- 2024-09-28Kotlin 如何增加、删除和修改 MutableStateFlow 中的值。-icode9专业技术文章分享
- 2024-09-28Kotlin的stateFlow.update 写法介绍-icode9专业技术文章分享
- 2024-09-28kotlin 怎么获取当前时间格式-icode9专业技术文章分享