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

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;

}





原文地址:https://www.cnblogs.com/meihao1203/p/8531980.html