多线程的同步与互斥---生产者与消费者模型

生产者与消费者模型是多线程同步与互斥应用的一个典型场景,在这个模型中我们要实现:

一个交易场所;

两种角色(一个是生产者,一个是消费者);

三种关系(互斥,同步,同步与互斥)。

值得注意的是,消费者与消费者,生产者与生产者之间都是互斥的关系;而生产者与消费者之间是同步与互斥的关系。说到这里就不免要解释一下线程的同步与互斥是什么了?简单来说,互斥就是同一个资源,互斥双方不能同时访问;而同步就是双方要相互合作按照某种顺序合作完成一个任务。

方法一:条件变量实现同步

这里首先采用互斥锁完成实现互斥关系,用条件变量完成同步关系(每生产一个数据,就通知一次消费者)。

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>

pthread_mutex_t mutex;
pthread_cond_t cond;

//实现交易场所(这里是一个链表)
typedef struct ListNode
{
    int data;
    struct ListNode* next;
}Node;
Node* head = NULL;
                                                                                                                                     
int count = 0;
//实现两种角色(即两个线程分别完成不同的工作)
void* Producer(void* arg)
{
    printf("hello Producer\n");
    while(1)
    {
        Node* new_node = (Node*)malloc(sizeof(Node));
        new_node->data = count++;                                                                                                    
        pthread_mutex_lock(&mutex);
        new_node->next = head;
        head = new_node;
        printf("Produce:%d\n", new_node->data);
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
    return NULL;
}
void* Consumer(void* arg)
{
    printf("hello Consumer\n");
    while(1)
    {
        if(head == NULL)
        {
            printf("no product to consume\n");
        }
        else if(head != NULL)
        {                                                                                                                            
            pthread_mutex_lock(&mutex);
            pthread_cond_wait(&cond, &mutex);
            printf("consume:%d\n", head->data);
            Node* todel = head;
            head = head->next;
            free(todel);
            pthread_mutex_unlock(&mutex);
        }
        sleep(1);
    }
    return NULL;
}
//实现三种关系
int main()
{
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

    pthread_t tid1;
    pthread_t tid2;
    pthread_t tid3;                                                                                                                  
    pthread_t tid4;
    pthread_create(&tid1, NULL, Producer, NULL);
    pthread_create(&tid2, NULL, Consumer, NULL);
    pthread_create(&tid3, NULL, Consumer, NULL);
    pthread_create(&tid4, NULL, Producer, NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    pthread_join(tid4, NULL);

    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mutex);
    return 0;
}

方法二:POSIX信号量实现同步

定义两个信号量,一个表示有多少空位置blank,一个表示数据的个数data;每次生产都要等待空位,也就是对blank进行P操作,每生产一次,就让数据的个数加1,也就是对data进行V操作;相反的在消费者线程中,要先对data进行P操作,在对blank进行V操作。

#include <stdio.h>                                                                                                                   
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

sem_t data;
sem_t blank;
pthread_mutex_t mutex;
//实现一个交易场所(使用数组实现一个队列)
#define MAXSIZE 10
int array[MAXSIZE] = {0};
int tail = 0;
int head = 0;
int count = 1;
//实现两个角色
void* producer(void* arg)
{
    while(1)
    {   
        sem_wait(&blank);
        pthread_mutex_lock(&mutex);
        array[tail++] = count;
        printf("produce data %d\n", count);
        count++;
        tail %= MAXSIZE;                                                                                                             
        pthread_mutex_unlock(&mutex);
        sem_post(&data);
        sleep(1);
    }

}
void* consumer(void* arg)
{
    while(1)
    {
        sem_wait(&data);
        pthread_mutex_lock(&mutex);
        printf("consume data %d\n", array[head++]);
        head %= MAXSIZE;
        pthread_mutex_unlock(&mutex);
        sem_post(&blank);
        sleep(1);
    }
}
int main()                                                                                                                           
{
    pthread_mutex_init(&mutex, NULL);
    sem_init(&blank, 0, MAXSIZE);
    sem_init(&data, 0, 0);

    pthread_t pid[4];
    int i = 0;
    for(i = 0; i < 2; ++i)
    {
        pthread_create(&pid[i], NULL, producer, NULL);
    }
    for(i = 2; i < 4; i++)
    {
        pthread_create(&pid[i], NULL, consumer, NULL);
    }
    for(i = 0; i < 4; ++i)
    {
        pthread_join(pid[i], NULL);
    }

    sem_destroy(&blank);                                                                                                             
    sem_destroy(&data);
    pthread_mutex_destroy(&mutex);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40417029/article/details/81116789