通过互斥锁Mutex和条件变量Conditions实现动态平衡,模拟生活中卖包子的情景

实现代码如下:

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

#define P_C 3 //3个生产者
#define C_C 2 //2个消费者 

int count = 0; //馒头的个数

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *consume(void *arg)
{
    while (1)
    {
        //观察是否有馒头可以吃,需要独占盆子
        pthread_mutex_lock(&mutex);
        while (count == 0)
        {
            //没有馒头可以吃,让阿姨生产
            printf("%lu cosumer is waitting\n", pthread_self());
            //去等着
            pthread_cond_wait(&cond, &mutex);
            //自动释放锁, 并且再条件变量上阻塞
            //唤醒的时候,自动去抢锁
        }
        //吃馒头
        count--;
        printf("%lu cosumer is consuming, count is %d\n", pthread_self(), count);
        //释放锁
        pthread_mutex_unlock(&mutex);
        sleep(2);
    }
    return NULL;
}

void *produce(void *arg)
{
    while (1)
    {
        //阿姨生产馒头, 独占盆子
        pthread_mutex_lock(&mutex);
        //盆子放的下才生产馒头
        if (count >= 5)
        {
            printf("too much waitting for consume\n");
            pthread_mutex_unlock(&mutex);
            sleep(1);
            continue;
        }
        printf("%lu starting produce, now count is %d\n", pthread_self(), count);
        count++;
        printf("after produce, count is %d\n", count);
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        sleep(2);
    }
}

int main()
{
    pthread_t pid[P_C], cid[C_C];

    int i;
    for (i = 0; i < C_C; i++)
    {
        if (0 != pthread_create(&cid[i], NULL, consume, NULL))
        {
            perror("c");
            exit(1);
        }
    }

    for (i = 0; i < P_C; i++)
    {
        if (0 != pthread_create(&pid[i], NULL, produce, NULL))
        {
            perror("p");
            exit(1);
        }
    }

    for (i = 0; i < C_C; i++)
    {
        pthread_join(cid[i], NULL);
    }

    for (i = 0; i < P_C; i++)
    {
        pthread_join(pid[i], NULL);
    }



    return 0;
}

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设有一个生产者-消费者模型,多个线程同时访问共享的缓冲区。为了保证线程安全,可以使用互斥锁条件变量来协调线程之间的操作。 在这个模型中,互斥锁用于保护共享资源,防止多个线程同时访问同一个资源。当一个线程需要访问共享资源时,需要先获取互斥锁,如果互斥锁已经被其他线程占用,那么当前线程将会被阻塞,直到互斥锁被释放。 下面是一个简单的示例代码: ```c++ #include <iostream> #include <pthread.h> #include <queue> using namespace std; queue<int> q; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void* producer(void* arg) { int count = 0; while (count < 10) { pthread_mutex_lock(&mutex); q.push(count++); cout << "Producer: produced " << count - 1 << endl; pthread_cond_signal(&cond); // 发送信号给消费者线程 pthread_mutex_unlock(&mutex); } return NULL; } void* consumer(void* arg) { while (true) { pthread_mutex_lock(&mutex); if (q.empty()) { pthread_cond_wait(&cond, &mutex); // 等待信号 } int value = q.front(); q.pop(); cout << "Consumer: consumed " << value << endl; pthread_mutex_unlock(&mutex); } return NULL; } int main() { pthread_t tid1, tid2; pthread_create(&tid1, NULL, producer, NULL); pthread_create(&tid2, NULL, consumer, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); return 0; } ``` 在这个示例中,生产者线程不断向队列中添加数据,每次添加完毕后发送一个信号给消费者线程,通知它们可以消费数据了。消费者线程在收到信号后,从队列中取出数据进行处理。如果队列为空,消费者线程将会等待信号,直到有新的数据可以消费。 在这个模型中,互斥锁保护了队列的访问,条件变量用于在生产者和消费者之间传递信号。通过互斥锁条件变量的协作,可以有效地保证多线程程序的正确性和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值