设为首页 加入收藏

TOP

Linux多线程编程之Java售票程序的移植
2014-11-24 11:39:35 来源: 作者: 【 】 浏览:0
Tags:Linux 线程 编程 Java 售票 程序 移植

不多解析了这个,直接贴Linux多线程编程之Java售票程序的移植代码.


开辟4个线程售票啊


#include
#include
int tickets=1000;
pthread_mutex_t mutex;
void *T_fun1()
{
while(tickets>0)
{
pthread_mutex_lock(&mutex);
printf("Linux thread 1 sales %d ticket\n",tickets);
tickets--;
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void *T_fun2()
{
while(tickets>0)
{


pthread_mutex_lock(&mutex);//锁定

printf("Linux thread 2 sales %d ticket\n",tickets);
tickets--;
pthread_mutex_unlock(&mutex);
sleep(1);
}

}
void *T_fun3()
{
while(tickets>0)
{


pthread_mutex_lock(&mutex);//互斥(chi)加锁

printf("Linux thread 3 sales %d ticket\n",tickets);
tickets--;
pthread_mutex_unlock(&mutex);//解锁互斥量
sleep(1);
}

}
void *T_fun4()
{
while(tickets>0)
{


pthread_mutex_lock(&mutex);//互斥(chi)加锁

printf("Linux thread 4 sales %d ticket\n",tickets);
tickets--;
pthread_mutex_unlock(&mutex);//解锁互斥量
sleep(1);
}

}
int main()
{
pthread_t id1;
pthread_t id2;
pthread_t id3;
pthread_t id4;
pthread_mutex_init(&mutex,NULL);
pthread_create(&id1,NULL,T_fun1,NULL);//参数3为线程运行函数起始地址
pthread_create(&id2,NULL,T_fun2,NULL);
pthread_create(&id3,NULL,T_fun3,NULL);
pthread_create(&id4,NULL,T_fun4,NULL);
sleep(100);//主进程休眠时间
printf("main sleep 100 second end\n");
return 0;
}



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android Recovery 代码分析 下一篇Java接收控制台数据Scanner类---..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)