互斥锁和条件变量在实际开发中的应用

来源:互联网 发布:小猪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(&register_lock,NULL);
     pthread_mutex_init(&login_lock,NULL);
     pthread_mutex_init(&play_lock,NULL);
     return;
}

//条件变量出始化
void init_pthread_cond()
{
     pthread_cond_init(&register_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(&register_lock);//上锁
         pthread_cond_wait(&register_cond,&register_lock);//等待信号
         pthread_mutex_unlock(&register_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(&register_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;
}

原创粉丝点击