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;

    }





  • 相关阅读:
    常用设计模式:装饰者模式
    常用数据结构算法 : 堆排序
    常用数据结构算法:二叉树的最近公共祖先
    java网络通信:HTTP协议 之 Sessions与Cookies
    java网络通信:HTTP协议
    常见的设计模式:工厂模式
    Java基础:类加载机制
    一个C++右值引用的问题
    剖析一个用C++写的行情交易系统
    C++ Coroutine简明教程
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8531980.html
Copyright © 2011-2022 走看看