六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 51|回复: 0

线程的helloworld

[复制链接]

升级  0.65%

321

主题

321

主题

321

主题

探花

Rank: 6Rank: 6

积分
1013
 楼主| 发表于 2013-1-26 12:27:01 | 显示全部楼层 |阅读模式
#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);}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表