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;

    }





  • 相关阅读:
    擦边上100分,我的托福考试总结
    如何写Java文档注释(Java Doc Comments)
    EC读书笔记系列之6:条款11 在operator=中处理自我赋值
    Python核心编程读笔 1
    安装Ubuntu小计
    U盘安装win7+CentOS7双系统
    EC读书笔记系列之5:条款9、条款10
    EC读书笔记系列之4:条款8 别让异常逃离析构函数
    EC读书笔记系列之3:条款5、条款6、条款7
    EC读书笔记系列之2:条款4 确定对象被使用前已先被初始化
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8531980.html
Copyright © 2011-2022 走看看