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

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

一个交易场所;

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

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

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

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

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

#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;
}

 

生产者消费者模型是计算机科学中一个经典的问题,也是进程同步互斥的一个重要应用。该模型描述了一个生产者和一个消费者共同使用一个有限缓冲区的情景,生产者向缓冲区中添加数据,而消费者则从缓冲区中取出数据。 为了避免在共享缓冲区时产生数据不一致的情况,需要进行进程同步互斥。其中,进程同步是指在多个进程之间协调事件发生的时间,而互斥则是指在同一时间只允许一个进程访问共享资源。 以下是一个基于信号量的生产者消费者模型的示例: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; // 缓冲区 int in = 0; // 生产者写入数据的位置 int out = 0; // 消费者读取数据的位置 sem_t empty; // 信号量,表示缓冲区中空余的位置数 sem_t full; // 信号量,表示缓冲区中已有数据的位置数 sem_t mutex; // 信号量,用于实现互斥访问缓冲区 void *producer(void *arg) { int item; while(1) { item = rand() % 1000; // 生产者随机生成一个数据项 sem_wait(&empty); // 等待缓冲区有空余位置 sem_wait(&mutex); // 进入互斥区,保证对缓冲区的访问是互斥的 buffer[in] = item; printf("Producer produces item %d at buffer[%d]\n", item, in); in = (in + 1) % BUFFER_SIZE; sem_post(&mutex); // 退出互斥区 sem_post(&full); // 增加已有数据的位置数 sleep(rand() % 2); // 生产者随机休眠一段时间 } } void *consumer(void *arg) { int item; while(1) { sem_wait(&full); // 等待缓冲区有数据 sem_wait(&mutex); // 进入互斥区 item = buffer[out]; printf("Consumer consumes item %d from buffer[%d]\n", item, out); out = (out + 1) % BUFFER_SIZE; sem_post(&mutex); // 退出互斥区 sem_post(&empty); // 增加空余位置数 sleep(rand() % 2); // 消费者随机休眠一段时间 } } int main() { pthread_t producer_thread, consumer_thread; sem_init(&empty, 0, BUFFER_SIZE); // 初始化信号量 sem_init(&full, 0, 0); sem_init(&mutex, 0, 1); pthread_create(&producer_thread, NULL, producer, NULL); // 创建生产者线程 pthread_create(&consumer_thread, NULL, consumer, NULL); // 创建消费者线程 pthread_join(producer_thread, NULL); // 等待生产者线程结束 pthread_join(consumer_thread, NULL); // 等待消费者线程结束 sem_destroy(&empty); // 销毁信号量 sem_destroy(&full); sem_destroy(&mutex); return 0; } ``` 在上述代码中,empty 表示缓冲区中空余的位置数,初始值为 BUFFER_SIZE;full 表示缓冲区中已有数据的位置数,初始值为 0;mutex 用于实现互斥访问缓冲区,初始值为 1。在生产者消费者访问缓冲区时,都需要先进行信号量操作,保证进程同步互斥。其中,sem_wait 和 sem_post 分别表示等待和增加信号量的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值