互斥锁和条件变量在实际开发中的应用
来源:互联网 发布:小猪php在线解密 编辑:程序博客网 时间:2024/05/29 17:56
//互斥锁和条件变量实际开发中应用
//下面是游戏的注册、登陆、玩游戏、停止、退出
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define USER_REGISTER 1
#define USER_LOGIN 2
#define USER_PLAY 3
#define USER_QUIT 4
#define USER_STOP 5
//1.互斥锁定义
pthread_mutex_t register_lock;
pthread_mutex_t login_lock;
pthread_mutex_t play_lock;
//条件变量定义
pthread_cond_t register_cond;
pthread_cond_t login_cond;
pthread_cond_t play_cond;
int flag = 0;
pthread_t tid[3] = {0};
//2.互斥锁的初始化
void init_pthread_lock()
{
pthread_mutex_init(®ister_lock,NULL);
pthread_mutex_init(&login_lock,NULL);
pthread_mutex_init(&play_lock,NULL);
return;
}
//条件变量出始化
void init_pthread_cond()
{
pthread_cond_init(®ister_cond,NULL);
pthread_cond_init(&login_cond,NULL);
pthread_cond_init(&play_cond,NULL);
return;
}
void *thread_register(void *arg)
{
while(1)
{
pthread_mutex_lock(®ister_lock);//上锁
pthread_cond_wait(®ister_cond,®ister_lock);//等待信号
pthread_mutex_unlock(®ister_lock);//解锁
while(flag)
{
puts("----------register-------------");
sleep(1);
}
}
}
void *thread_login(void* arg)
{
while(1)
{
pthread_mutex_lock(&login_lock);
pthread_cond_wait(&login_cond,&login_lock);
pthread_mutex_unlock(&login_lock);
while(flag)
{
puts("-----------login---------------");
sleep(1);
}
}
}
void *thread_play(void *arg)
{
while(1)
{
pthread_mutex_lock(&play_lock);
pthread_cond_wait(&play_cond,&play_lock);
pthread_mutex_unlock(&play_lock);
while(flag)
{
puts("-----------play--------------");
sleep(1);
}
}
}
void create_thread()
{
int ret = 0;
ret = pthread_create(&tid[0],NULL,thread_register,NULL);
if(ret != 0)
{
fprintf(stderr,"fail to pthread_create:%s\n",strerror(ret));
exit(EXIT_FAILURE);
}
ret = pthread_create(&tid[1],NULL,thread_login,NULL);
if(ret != 0)
{
fprintf(stderr,"fail to pthread_create:%s\n",strerror(ret));
exit(EXIT_FAILURE);
}
ret = pthread_create(&tid[2],NULL,thread_play,NULL);
if(ret != 0)
{
fprintf(stderr,"fail to pthread_create:%s",strerror(ret));
exit(EXIT_FAILURE);
}
}
int main(int argc, const char *argv[])
{
int cmd = 0,i = 0;
init_pthread_lock();
init_pthread_cond();
create_thread();
while(1)
{
printf("please input [1-5] select [register,login,game,quit,stop:\n");
scanf("%d",&cmd);
//getchar();
switch(cmd)
{
case USER_REGISTER:
flag = 1;
pthread_cond_signal(®ister_cond);//发送信号
break;
case USER_LOGIN:
flag = 1;
pthread_cond_signal(&login_cond);
break;
case USER_PLAY:
pthread_cond_signal(&play_cond);
flag = 1;
break;
case USER_QUIT:
goto loop;
break;
case USER_STOP:
flag = 0;
break;
}
}
loop:
for(i = 0;i < 3;i++)
{
pthread_cancel(tid[i]);
}
printf("GAME OVER!\n");
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
pthread_join(tid[2],NULL);
exit(EXIT_SUCCESS);
return 0;
}
阅读全文
0 0
- 互斥锁和条件变量在实际开发中的应用
- UML在实际开发中的应用
- 数据结构在实际开发过程中的应用
- EL在实际开发中的应用举例
- MD5在实际开发中的应用方式
- Runtime在实际开发中的应用
- vector在实际多线程开发中的应用
- Java 反射在实际开发中的应用
- Java 反射在实际开发中的应用
- python3之数字(在实际应用中的数字--变量和基本表达式)
- 快速原型开发模式在实际开发过程中的应用
- 快速原型开发模式在实际开发过程中的应用
- 条件变量 condition 在 python 多线程编程中的应用
- 浅谈实际开发中数据源在JDBC中的应用
- SCA技术在实际应用开发中的实践(1)-序
- 浅谈实际开发中数据源在JDBC中的应用
- 成员函数指针在开发中的实际应用
- Oracle 临时表在实际开发中的应用
- Ubuntu 建立subversion与redmine关联
- 032day(STL中的二分查找算法)
- 基于JavaMail的Java邮件发送:简单邮件发送
- hdu 4310
- verilog编译指令
- 互斥锁和条件变量在实际开发中的应用
- Python3 简单抓取网站url
- Maven基础
- Sass的使用(让css更优雅和强大)---第一节
- SQL之数据库的定义与基本操作
- 为什么i++后,i++的值不变
- Mybatis学习笔记(十三)【多对多查询】
- 软件架构师知识点2
- 有关map集合的理解