zoukankan      html  css  js  c++  java
  • 线程同步互斥实现资源访问

    3个线程,两个线程进行卖票操作,初始票数5张,一个线程在第一个10张票卖完的情况下重新设定票数为5,然后自己退出。
    #include<stdio.h>
    #include<pthread.h>
    #include<unistd.h>
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    static int ticketcount = 5;
    void *pthFunc1(void *args)
    {
            while(1)
            {
                    pthread_mutex_lock(&mutex);
                    if(ticketcount>0)
                    {
                            printf("thread 1 sell ticket , ticket number is %d ",ticketcount);
                            ticketcount--;
                            if(0==ticketcount)
                            {
                                    pthread_cond_signal(&cond);
                            }
                            printf("after sell , remain ticket is %d ",ticketcount);
                    }
                    else
                    {
                            pthread_mutex_unlock(&mutex);
                            pthread_exit(NULL);
                    }
                    pthread_mutex_unlock(&mutex);
                    sleep(1);
            }
    }
    void *pthFunc2(void *args)
    {
            while(1)
            {
                    pthread_mutex_lock(&mutex);
                    if(ticketcount>0)
                    {
                            printf("thread 2 sell ticket , ticket number is %d ",ticketcount);
                            ticketcount--;
                            if(0==ticketcount)
                            {
                                    pthread_cond_signal(&cond);
                            }
                            printf("after sell , remain ticket is %d ",ticketcount);
                    }
                    else
                    {
                            pthread_mutex_unlock(&mutex);
                            pthread_exit(NULL);
                    }
                    pthread_mutex_unlock(&mutex);
                    sleep(1);
            }
    }
    void *pthFunc3(void *args)
    {
            pthread_mutex_lock(&mutex);
            pthread_cond_wait(&cond,&mutex);
            ticketcount = 5;
            pthread_mutex_unlock(&mutex);
            pthread_exit(NULL);
    }
    int main(void)
    {
            pthread_t pthId[3];
            pthread_mutex_init(&mutex,NULL);
            pthread_cond_init(&cond,NULL);
            pthread_create(&pthId[0],NULL,pthFunc1,NULL);
            pthread_create(&pthId[1],NULL,pthFunc2,NULL);
            pthread_create(&pthId[2],NULL,pthFunc3,NULL);
            pthread_join(pthId[0],NULL);
            pthread_join(pthId[1],NULL);
            pthread_join(pthId[2],NULL);
            pthread_mutex_destroy(&mutex);
            pthread_cond_destroy(&cond);
            return 0;

    }





  • 相关阅读:
    4种方法帮你解决IntelliJ IDEA控制台中文乱码问题
    万字长文:解读区块链7类共识算法
    CoralCache:一个提高微服务可用性的中间件
    探究Python源码,终于弄懂了字符串驻留技术
    OAuth:每次授权暗中保护你的那个“MAN”
    厉害了!这群95后正在用三维成像技术让科幻变成现实
    华为云FusionInsight MRS在金融行业存算分离的实践
    【新春特辑】发压岁钱、看贺岁片、AI写春联……华为云社区给大家拜年了
    Java实现 蓝桥杯 算法训练 天数计算
    WebRTC框架中的硬件加速
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8531980.html
Copyright © 2011-2022 走看看