生产者消费者模型的两种C语言实现

使用互斥锁和条件变量实现的生产者和消费者模型

#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
#include "unistd.h"

/*使用互斥锁和条件变量实现的生产者和消费者模型*/

pthread_mutex_t mutex;
pthread_cond_t cond_prod;
pthread_cond_t cond_cons;

#define MAX_SIZE 10
int buffer[MAX_SIZE];
int count = 0;

void* producer(void *arg) {
    while (1)
    {
        int data = rand() % 100;
        pthread_mutex_lock(&mutex);
        while (count == MAX_SIZE)
        {
            pthread_cond_wait(&cond_prod, &mutex); // 阻塞等待
        }
        buffer[count++] = data;
        printf("producer data: %d\n", data);
        pthread_mutex_unlock(&mutex);

        pthread_cond_signal(&cond_cons); // 唤醒一个消费者

        usleep(500 * 1000);
    }
    
    return NULL;
}

void* consumer(void *arg) {
    while (1)
    {
        pthread_mutex_lock(&mutex);
        while (count == 0)
        {
            pthread_cond_wait(&cond_cons, &mutex); // 阻塞等待
        }
        int data = buffer[--count];
        printf("consumer data: %d\n", data);
        pthread_mutex_unlock(&mutex);

        pthread_cond_signal(&cond_prod);

        usleep(500 * 1000);
    }
    
    return NULL;
}

int main() {
    srand(time(NULL));

    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond_prod, NULL);
    pthread_cond_init(&cond_cons, NULL);

    pthread_t prod_thread, cons_thread;
    pthread_create(&prod_thread, NULL, producer, NULL);
    pthread_create(&cons_thread, NULL, consumer, NULL);

    pthread_join(prod_thread, NULL);
    pthread_join(cons_thread, NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond_prod);
    pthread_cond_destroy(&cond_cons);

    return 0;
}

使用互斥锁和信号量实现的生产者和消费者模型

#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
#include "unistd.h"
#include "semaphore.h"

/*使用互斥锁和信号量实现的生产者和消费者模型*/

pthread_mutex_t mutex;
sem_t sem_empty, sem_full;

#define MAX_SIZE 10
int buffer[MAX_SIZE];
int count = 0;

void* producer(void *arg) {
    while (1)
    {
        int data = rand() % 100;
        sem_wait(&sem_empty);
        pthread_mutex_lock(&mutex);

        buffer[count++] = data;
        printf("producer data: %d\n", data);
        pthread_mutex_unlock(&mutex);

        sem_post(&sem_full);

        usleep(500 * 1000);
    }
    
    return NULL;
}

void* consumer(void *arg) {
    while (1)
    {
        sem_wait(&sem_full);
        pthread_mutex_lock(&mutex);
        int data = buffer[--count];
        printf("consumer data: %d\n", data);
        pthread_mutex_unlock(&mutex);

        sem_post(&sem_empty);

        usleep(500 * 1000);
    }
    
    return NULL;
}

int main() {
    srand(time(NULL));

    pthread_mutex_init(&mutex, NULL);
    sem_init(&sem_empty, 0, MAX_SIZE);
    sem_init(&sem_full, 0, 0);

    pthread_t prod_thread, cons_thread;
    pthread_create(&prod_thread, NULL, producer, NULL);
    pthread_create(&cons_thread, NULL, consumer, NULL);

    pthread_join(prod_thread, NULL);
    pthread_join(cons_thread, NULL);

    pthread_mutex_destroy(&mutex);
    sem_destroy(&sem_empty);
    sem_destroy(&sem_full);

    return 0;
}

信号量的优点:

信号量是一种底层的同步原语,可以被用来实现各种同步机制。
信号量可以用于解决多种同步问题,包括生产者消费者问题。
信号量的实现简单,易于理解和调试。

信号量的缺点:

信号量不能被用来解决所有的同步问题,因为它们只是原子计数器,并不能处理数据传输等高级同步问题。
如果信号量的使用不当,可能会导致死锁或竞态条件。

应用场景:

生产者消费者问题:信号量可以用于实现生产者消费者问题。生产者线程使用信号量来控制缓冲区空闲的空间数量,消费者线程使用信号量来控制缓冲区中已有数据的数量。
限制访问资源:信号量可以用于限制对共享资源的并发访问。例如,限制最多同时有多少个线程可以访问共享数据结构。
控制线程执行顺序:信号量可以用于控制线程的执行顺序。例如,使用信号量来实现线程池中的任务调度。

条件变量的优点:

条件变量提供了一种高级的同步原语,可以用于解决多种同步问题,包括生产者消费者问题。
条件变量提供了更高级的同步抽象,可以方便地处理数据传输等高级同步问题。
条件变量可以使得线程在等待某个条件成立时进入阻塞状态,从而节省了 CPU 资源。

条件变量的缺点:

条件变量的使用需要与互斥锁配合使用,容易出现死锁或竞态条件。
条件变量的实现相对复杂,容易引入 bug。

应用场景:

生产者消费者问题:条件变量可以用于实现生产者消费者问题。生产者线程在生产完一个产品后,会发送一个信号通知消费者线程消费,消费者线程会等待这个信号,直到有产品可消费为止。
线程池的任务调度:线程池中的任务队列可以使用条件变量来实现,当任务队列为空时,线程可以等待条件变量的信号,直到有新任务被加入到队列中为止。
定时等待:条件变量可以用于实现定时等待
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值