• 线程同步互斥实现资源访问


    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;

    }





  • 相关阅读:
    jQuery学习教程(一):入门
    jQuery学习教程(八):事件
    jQuery学习教程(五):选择器综合实例
    jQuery学习教程(六):属性操作与CSS操作
    jQuery学习教程(四):使用jQuery操作DOM
    jQuery学习教程(七):val()与节点操作
    jQuery学习教程(二):选择器1
    const的使用
    ASP.NET 页面间传值的方法
    .net中接口与基类
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8531980.html
Copyright © 2020-2023  润新知