#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
pthread_t appId,bnaId,cryId;
int ret;
int status=1;
void apple(){
while (1){
if (status==1){
status=2;
printf("apple\n");
}
pthread_yield(bnaId,NULL);
}
}
void banana(){
while (1){
if (status==2){
status=3;
printf("banana\n");
}
pthread_yield(cryId,NULL);
}
}
void cherry(){
while (1){
if (status==3){
status=1;
printf("cherry\n");
}
pthread_yield(appId,NULL);
}
}
int main(){
ret=pthread_create(&appId,NULL,(void*)apple,NULL);
if (ret!=0){
printf("Creat Apple pthread error!\n");
exit(1);
}
ret=pthread_create(&bnaId,NULL,(void*)banana,NULL);
if (ret!=0){
printf("Creat Banana pthread error!\n");
exit(1);
}
ret=pthread_create(&cryId,NULL,(void*)cherry,NULL);
if (ret!=0){
printf("Creat Cherry pthread error!\n");
exit(1);
}
pthread_join(appId,NULL);
}
线程的helloworld
最新推荐文章于 2020-04-27 13:07:00 发布
本文展示了一个使用C语言和POSIX线程库实现的简单多线程程序。该程序通过三个线程按顺序打印“apple”、“banana”和“cherry”,展示了线程之间的同步与调度。
4418

被折叠的 条评论
为什么被折叠?



