Linux C/C++多线程学习:生产者消费者问题

14 篇文章 0 订阅

生产者消费者问题

多个生产者和多个生产者的问题。生产者不断的向仓库放入产品,消费者不断的从仓库取出产品,仓库的容量是有限的。因此,当仓库处于满状态时,生产者必须等待消费者取出 1 个或多个产品后才能继续生产;同理,当仓库处于空状态时,消费者必须等待生产者将产品放入仓库后才能消费(取出)产品。

使用数组模拟仓库,需要记录下一次生产和消费在数组中的位置。 
用生产和消费在数组中的位置判断仓库是否为空或者为满: 
假设仓库容量为 N:

  • (produce_position+1)%N == consume_position 满 
    因为初始位置都是 0,当两者相差一个位置时,定义满状态。(最多存储N-1个)
  • consume_position == produce_position 空

当仓库满时,阻塞生产者;当一个消费行为后,仓库非满,唤醒生产者; 
当仓库空时,阻塞消费者;当一个生产行为后,仓库非空,唤醒消费者; 
因此需要引入,仓库非满条件变量仓库非空条件变量

由于生产和消费行为都会修改数据,因此两者操作必须互斥,需引入生产消费互斥锁。 
当我们要生产(或消费)一定数量的产品时,需要计数判断是否已经完成工作;多个生产者进行生产时,都会对生产的计数变量进行修改,因此需引入生产计数互斥锁消费计数互斥锁,保证同时只有一个生产(或消费)进程对计数变量进行修改。


C实现:单个生产者,单个消费者

//信号量---线程间通信
//“生产者消费者” 问题
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#define msleep(x) usleep(x*1000)
#define PRODUCT_SPEED 1     //生产速度
#define CONSUM_SPEED  3     //消费速度
#define INIT_NUM      3     //仓库原有产品数
#define TOTAL_NUM     10    //仓库容量

sem_t p_sem, c_sem, sh_sem;
int num=INIT_NUM;

//生产产品
void product(void)
{
    sleep(PRODUCT_SPEED);
}

//添加产品到仓库
int add_to_lib()
{
    num++;                  //仓库中的产品增加一个
    msleep(500);
    return num;
}

//消费
void consum()
{
    sleep(CONSUM_SPEED);
}

//从仓库中取出产品
int sub_from_lib()
{
    num--;                  //仓库中的产品数量减一
    msleep(500);
    return num;
}

//生产者线程
void *productor(void *arg)
{
    while(1)
    {
        sem_wait(&p_sem);   //生产信号量减一
        product();          //生产延时
        sem_wait(&sh_sem);  //这个信号量是用来互斥的
        printf("push into! tatol_num=%d\n", add_to_lib());
        sem_post(&sh_sem);
        sem_post(&c_sem);   //消费信号量加一
    }
}

//消费者线程
void *consumer(void *arg)
{
    while(1)
    {

        sem_wait(&c_sem);   //消费者信号量减一
        sem_wait(&sh_sem);
        printf("pop out! tatol_num=%d\n", sub_from_lib());
        sem_post(&sh_sem);
        sem_post(&p_sem);   //生产者信号量加一
        consum();           //消费延时
    }
}

int main()
{
    pthread_t tid1,tid2;

    sem_init(&p_sem,0,TOTAL_NUM-INIT_NUM);
    sem_init(&c_sem,0,INIT_NUM);
    sem_init(&sh_sem,0,1);

    pthread_create(&tid1,NULL,productor,NULL);
    pthread_create(&tid2,NULL,consumer,NULL);

    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

    return 0;
}

C++实现:多个生产者,多个消费者

  1. C++11 线程库:http://zh.cppreference.com/w/cpp/thread

  2. 互斥量和锁
    std::unique_lock::lock 和 std::unique_lock::unlock 
    上锁操作,调用它所管理的 Mutex 对象的 lock 函数。如果在调用 Mutex 对象的 lock 函数时该 Mutex 对象已被另一线程锁住,则当前线程会被阻塞,直到它获得了锁。

  3. 条件变量 condition_variable

    • void wait (unique_lock& lck); 无条件被阻塞。调用该函数前,当前线程应该已经对unique_lock lck完成了加锁。所有使用同一个条件变量的线程必须在wait函数中使用同一个unique_lock。该wait函数内部会自动调用lck.unlock()对互斥锁解锁,使得其他被阻塞在互斥锁上的线程恢复执行。使用本函数被阻塞的当前线程在获得通知(notified,通过别的线程调用 notify_*系列的函数)而被唤醒后wait()函数恢复执行并自动调用lck.lock()对互斥锁加锁

    • template void wait (unique_lock& lck, Predicate pred);带条件的被阻塞。wait函数设置了谓词(Predicate),只有当pred条件为false时调用该wait函数才会阻塞当前线程,并且在收到其他线程的通知后只有当pred为true时才会被解除阻塞。因此,等效于while (!pred()) wait(lck);

  4. 线程入口函数 
    一般情况下,我们使用全局函数或者类的静态函数作为线程函数入口;但是以上2者都不能访问类的非静态成员变量。 
    使用类的成员函数,作为线程入口函数,使该线程入口函数能够访问类的成员变量。
class MyClass {
    int fun(int a) {
    ...
    }
}
int main() {
    MyClass MyObject;
    int a = 2;
    // 第一个参数传入函数名,第二个参数传入类实例的地址(this), 第三个参数开始传入函数参数;
    thread my_thread(MyClass::fun, &MyObject, a); 
}

注意:printf是线程安全的,而cout不是!
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <unistd.h>
#include <thread>
using namespace std;

const int kProduceItems = 10;
const int kRepositorySize = 4;

template<class T>
class Repository {
public:
    T items_buff[kRepositorySize];
    mutex mtx; // 生产者消费者互斥量
    mutex produce_mutex; // 生产计数互斥量
    mutex consume_mutex; // 消费计数互斥量
    size_t produce_item_count;
    size_t consume_item_count;
    size_t produce_position; // 下一个生产的位置
    size_t consume_position; // 下一个消费的位置
    condition_variable repo_not_full; // 仓库不满条件变量
    condition_variable repo_not_empty; // 仓库不空条件变量

    Repository() {
        produce_item_count = 0;
        consume_item_count = 0;
        produce_position = 0;
        consume_position = 0;
    };

    void Init() {
        fill_n(items_buff, sizeof(items_buff)/sizeof(items_buff[0]), 0);
        produce_item_count = 0;
        consume_item_count = 0;
        produce_position = 0;
        consume_position = 0;
    }
};

template<class T>
class Factory {
private:
    Repository<T> repo;

    void ProduceItem(T item) {
        unique_lock<mutex> lock(repo.mtx);
        // +1 后判断,因为在初始时,两者位于同一位置(因此仓库中最大存在 kRepositorySize-1 个产品)
        while ((repo.produce_position+1) % kRepositorySize == repo.consume_position) {
            cout << "Repository is full, waiting..." << endl;
            (repo.repo_not_full).wait(lock); // 阻塞时释放锁,被唤醒时获得锁
        }
        repo.items_buff[repo.produce_position++] = item;
        if (repo.produce_position == kRepositorySize)
            repo.produce_position = 0;
        (repo.repo_not_empty).notify_all(); // 唤醒所有因空阻塞的进程
        lock.unlock();
    }

    T ConsumeItem() {
        unique_lock<mutex> lock(repo.mtx);
        while (repo.consume_position == repo.produce_position) {
            cout << "Repository is empty, waiting ..." << endl;
            (repo.repo_not_empty).wait(lock);
        }
        T data = repo.items_buff[repo.consume_position++];
        if (repo.consume_position == kRepositorySize)
            repo.consume_position = 0;
        (repo.repo_not_full).notify_all();
        lock.unlock();
        return data;
    }

public:
    void Reset() {
        repo.Init();
    }

    void ProduceTask() {
        bool ready_to_exit = false;
        while (true) {
            sleep(1); // 如果不sleep ,运行太快,一个进程会完成所有生产
            unique_lock<mutex> lock(repo.produce_mutex);

            if (repo.produce_item_count < kProduceItems) {
                ++(repo.produce_item_count);
                T item = repo.produce_item_count;
                cout << "producer id: "<< this_thread::get_id() << " is producing "
                     << item << "^th item..." << endl;
                ProduceItem(item);
            } else {
                ready_to_exit = true;
            }

            lock.unlock();
            // sleep(1);
            if (ready_to_exit)
                break;
        }
        printf("Producer thread %lld is exiting...\n", std::this_thread::get_id());
    }

    void ConsumeTask() {
        bool ready_to_exit =false;
        while (true) {
            sleep(1); // 如果不sleep ,运行太快,一个进程会消费所有产品
            unique_lock<mutex> lock(repo.consume_mutex);

            if (repo.consume_item_count < kProduceItems) {
                T item = ConsumeItem();
                cout << "consumer id: " << this_thread::get_id() << " is consuming "
                     << item << "^th item" << endl;
                ++(repo.consume_item_count);
            } else {
                ready_to_exit = true;
            }

            lock.unlock();
            // sleep(1);
            if (ready_to_exit)
                break;
        }
        printf("Consumer thread %lld is exiting...\n", std::this_thread::get_id());
    }
};

int main() {
    cout << "Main thread id :" << this_thread::get_id() << endl;

    Factory<int> myfactory;

    thread producer1(&Factory<int>::ProduceTask, &myfactory);
    thread producer2(&Factory<int>::ProduceTask, &myfactory);
    thread producer3(&Factory<int>::ProduceTask, &myfactory);

    thread consumer1(&Factory<int>::ConsumeTask, &myfactory);
    thread consumer2(&Factory<int>::ConsumeTask, &myfactory);
    thread consumer3(&Factory<int>::ConsumeTask, &myfactory);

    producer1.join();
    producer2.join();
    producer3.join();

    consumer1.join();
    consumer2.join();
    consumer3.join();

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EnjoyCodingAndGame

愿我的知识,成为您的财富!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值