买票系统
来源:互联网 发布:淘宝投诉假货流程 编辑:程序博客网 时间:2024/05/01 21:10
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- //#include <unistd.h>
- //#include <errno.h>
- #include <time.h>
- pthread_mutex_t mutex;
- pthread_cond_t cond1;
- pthread_cond_t cond2;
- pthread_cond_t cond3;
- int ticket = 10;
- void *sell_ticket1(void)
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&cond1,&mutex);
- if(ticket > 0)
- {
- printf("sell ticket1--> chool ticket:%d\n",ticket--);
- //sleep(1);
- pthread_mutex_unlock(&mutex);
- }
- else
- {
- pthread_mutex_unlock(&mutex);
- break;
- }
- }
- return NULL;
- }
- void *sell_ticket2(void)
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&cond2,&mutex);
- if(ticket > 0)
- {
- //sleep(1);
- printf("sell ticket2--> chool ticket:%d\n",ticket--);
- //sleep(1);
- pthread_mutex_unlock(&mutex);
- }
- else
- {
- pthread_mutex_unlock(&mutex);
- break;
- }
- }
- }
- void *sell_ticket3(void)
- {
- while(1)
- {
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&cond3,&mutex);
- if(ticket > 0)
- {
- //sleep(1);
- printf("sell ticket3--> chool ticket:%d\n",ticket--);
- //sleep(1);
- pthread_mutex_unlock(&mutex);
- }
- else
- {
- pthread_mutex_unlock(&mutex);
- break;
- }
- }
- return NULL;
- }
- int main()
- {
- int i=0,flog=0,ret=0;
- pthread_t id1,id2,id3;
- int old_ticket = ticket;
- srand(time(NULL));
- pthread_mutex_init(&mutex,NULL);
- pthread_cond_init(&cond1,NULL);
- pthread_cond_init(&cond2,NULL);
- pthread_cond_init(&cond3,NULL);
- ret = pthread_create(&id1, NULL, (void*)sell_ticket1, NULL);
- if(ret == -1)
- {
- printf("create_thread1 error!\n");
- exit(-1);
- }
- ret = pthread_create(&id2, NULL, (void*)sell_ticket2, NULL);
- if(ret == -1)
- {
- printf("create_thread2 error!\n");
- exit(-1);
- }
- ret = pthread_create(&id3, NULL, (void*)sell_ticket3, NULL);
- if(ret == -1)
- {
- printf("create_thread3 error!\n");
- exit(-1);
- }
- sleep(1);
- for(i =0; i< old_ticket; i++)
- {
- flog = rand() % 3;
- //printf("%d\n",flog);
- switch(flog)
- {
- case 0: pthread_cond_signal(&cond1);break;
- case 1: pthread_cond_signal(&cond2);break;
- case 2: pthread_cond_signal(&cond3);break;
- }
- sleep(1);
- }
- pthread_cond_signal(&cond1);
- sleep(1);
- pthread_cond_signal(&cond2);
- sleep(1);
- pthread_cond_signal(&cond3);
- sleep(1);
- ret = pthread_join(id1, NULL);
- if(ret == -1)
- {
- printf("join_thread1 error!\n");
- exit(-1);
- }
- printf("id1 exit!\n");
- ret = pthread_join(id2, NULL);
- if(ret == -1)
- {
- printf("join_thread2 error!\n");
- exit(-1);
- }
- printf("id2 exit!\n");
- ret = pthread_join(id3, NULL);
- if(ret == -1)
- {
- printf("join_thread3 error!\n");
- exit(-1);
- }
- printf("id3 exit!\n");
- return 0;
- }
0 0
- 买票系统
- java 简单买票系统
- oc编程:电影院买票系统2-----代码实现
- 买票系统模拟Java高并发(ConcurrentQueue的解决方案)
- 沁园春-买票
- 沁园春 买票
- 买票难
- 买票找零
- 沁园春,买票
- 买票找零
- 买票事务
- 买票找零
- 买票问题
- 买票找零
- codevs 买票
- 【codevs1666】买票
- 排队买票
- 排队买票
- 局部变量作用域,方法编写的注意事项:或者说是一些原则
- [LeetCode]Word Break
- 改变超链接样式
- NPOI 字体
- struct 相关
- 买票系统
- checkSelfPermission 失效
- Shell部分14
- 线程练习
- hdoj 1847 Good Luck in CET-4 Everybody!(博弈, sg函数)
- Ajax三级联动
- 《合成孔径雷达成像——算法与实现》之【10】仿真图3.13
- PHP中9大缓存技术总结
- Android 布局属性layout_weight不起作用?