线程的helloworld

#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);
}

猜你喜欢

转载自haoningabc.iteye.com/blog/1709157
今日推荐