C++多线程生产者消费者的实现

分为四种情况:单生产者单消费者;单生产者多消费者;多生产者单消费者;多生产者多消费者。

单生产者单消费者:

#include<iostream>
#include<chrono>
#include<mutex>
#include<condition_variable>
#include<thread>


using namespace std;

static int pool_max = 10, max_size = 15, pool_size = 0, producer_count = 0, consumer_count = 0;
    /* 产品池容量大小   最多生产产品的个数 当前池中的产品个数 生产了个产品数量  消费了的产品数量 */

mutex m_c, m_p, m;
/* 多消费者时的互斥量   多生产者时的互斥量  产品池的互斥量 */

condition_variable p_not_full, p_not_empty;
/* 生产者等待产品池不满   消费者等待产品池非空 */


void ProducerS ()
{
    while (true)
    {
        unique_lock<mutex> lck(m);
        while (pool_size == pool_max)
        {
            cout << "Pool full\t Wait..." << endl;
            p_not_full.wait(lck);
        }
        pool_size++;
        producer_count++;
        cout << "Producer generate : " << producer_count << endl;
        if (pool_size == 1)
        {
            cout << "Notify consumer...\n";
            p_not_empty.notify_one();
        }
        if (producer_count == max_size) break;
        lck.unlock();
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
    cout << "Producer Single exit\t id is : " << this_thread::get_id() << endl;
}

void ConsumerS()
{
    while (true)
    {
        unique_lock<mutex> lck(m);
        while (pool_size == 0)
        {
            cout << "Pool empty\t Wait...\n";
            p_not_empty.wait(lck);
        }
        pool_size--;
        consumer_count++;
        cout << "Consumer consume : " << consumer_count << endl;
        if (pool_size == pool_max-1)
        {
            cout << "Notify producer...\n";
            p_not_full.notify_one();
        }
        if (consumer_count == max_size) break;
        lck.unlock();
        this_thread::sleep_for(chrono::milliseconds(1500));
    }
    cout << "Consumer Single exit\t id is : " << this_thread::get_id() << endl;
}

void TestPsCs()
{
    thread t_c(ConsumerS);
    thread t_p(ProducerS);

    t_c.join();
    t_p.join();
}


int main(void)
{
    TestPsCs();
    return 0;
}

运行截图:

单生产者多消费者:

#include<iostream>
#include<chrono>
#include<mutex>
#include<condition_variable>
#include<thread>


using namespace std;

static int pool_max = 10, max_size = 15, pool_size = 0, producer_count = 0, consumer_count = 0;
    /* 产品池容量大小   最多生产产品的个数 当前池中的产品个数 生产了个产品数量  消费了的产品数量 */

mutex m_c, m_p, m;
/* 多消费者时的互斥量   多生产者时的互斥量  产品池的互斥量 */

condition_variable p_not_full, p_not_empty;
/* 生产者等待产品池不满   消费者等待产品池非空 */


void ProducerS ()
{
    while (true)
    {
        unique_lock<mutex> lck(m);
        while (pool_size == pool_max)
        {
            cout << "Pool full\t Wait..." << endl;
            p_not_full.wait(lck);
        }
        pool_size++;
        producer_count++;
        cout << "Producer generate : " << producer_count << endl;
        if (pool_size == 1)
        {
            cout << "Notify consumer...\n";
            p_not_empty.notify_one();
        }
        if (producer_count == max_size) break;
        lck.unlock();
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
    cout << "Producer Single exit\t id is : " << this_thread::get_id() << endl;
}

void ConsumerM()
{
    while (true)
    {
        unique_lock<mutex> lck_c(m_c);
        if (consumer_count == max_size) break;

        unique_lock<mutex> lck(m);
        while (pool_size == 0)
        {
            cout << "Pool empty\t Wait...\n";
            p_not_empty.wait(lck);
        }
        pool_size--;
        consumer_count++;
        cout << "Consumer : " << this_thread::get_id() << "\tconsume : " << consumer_count << endl;
        if (pool_size == pool_max-1)
        {
            cout << "Notify producer...\n";
            p_not_full.notify_one();
        }
        if (consumer_count == max_size) break;
        lck_c.unlock();
        lck.unlock();
        this_thread::sleep_for(chrono::milliseconds(1500));
    }
    cout << "Consumer Single exit\t id is : " << this_thread::get_id() << endl;
}


void TestPsCM()
{
    const int c_t_i = 5;
    thread t_c[c_t_i];
    for (int i = 0; i<c_t_i; ++i)
        t_c[i] = thread(ConsumerM);
    thread t_p(ProducerS);

    for (int i = 0; i<c_t_i; ++i)
        t_c[i].join();
    t_p.join();
}


int main(void)
{
    TestPsCM();
    return 0;
}

运行截图:

多生产者单消费者:

#include<iostream>
#include<chrono>
#include<mutex>
#include<condition_variable>
#include<thread>


using namespace std;

static int pool_max = 10, max_size = 15, pool_size = 0, producer_count = 0, consumer_count = 0;
    /* 产品池容量大小   最多生产产品的个数 当前池中的产品个数 生产了个产品数量  消费了的产品数量 */

mutex m_c, m_p, m;
/* 多消费者时的互斥量   多生产者时的互斥量  产品池的互斥量 */

condition_variable p_not_full, p_not_empty;
/* 生产者等待产品池不满   消费者等待产品池非空 */


void ProducerM ()
{
    while (true)
    {
        unique_lock<mutex> lck_p(m_p);
        if (producer_count == max_size) break;

        unique_lock<mutex> lck(m);
        while (pool_size == pool_max)
        {
            cout << "Pool full\t Wait..." << endl;
            p_not_full.wait(lck);
        }
        pool_size++;
        producer_count++;
        cout << "Producer : " << this_thread::get_id() << "\tgenerate : " << producer_count << endl;
        if (pool_size == 1)
        {
            cout << "Notify consumer...\n";
            p_not_empty.notify_one();
        }
        if (producer_count == max_size) break;
        lck.unlock();
        lck_p.unlock();
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
    cout << "Producer Single exit\t id is : " << this_thread::get_id() << endl;
}

void ConsumerS()
{
    while (true)
    {
        unique_lock<mutex> lck(m);
        while (pool_size == 0)
        {
            cout << "Pool empty\t Wait...\n";
            p_not_empty.wait(lck);
        }
        pool_size--;
        consumer_count++;
        cout << "Consumer : " << this_thread::get_id() << "\tconsume : " << consumer_count << endl;
        if (pool_size == pool_max-1)
        {
            cout << "Notify producer...\n";
            p_not_full.notify_one();
        }
        if (consumer_count == max_size) break;
        lck.unlock();
        this_thread::sleep_for(chrono::milliseconds(1500));
    }
    cout << "Consumer Single exit\t id is : " << this_thread::get_id() << endl;
}


void TestPmCs()
{
    const int p_t_i = 5;
    thread t_c(ConsumerS);
    thread t_p[p_t_i];
    for (int i = 0; i<p_t_i; ++i)
        t_p[i] = thread(ProducerM);

    for (int i = 0; i<p_t_i; ++i)
        t_p[i].join();
    t_c.join();
}


int main(void)
{
    TestPmCs();
    return 0;
}

运行截图:

多生产者多消费者:

#include<iostream>
#include<chrono>
#include<mutex>
#include<condition_variable>
#include<thread>


using namespace std;

static int pool_max = 10, max_size = 15, pool_size = 0, producer_count = 0, consumer_count = 0;
    /* 产品池容量大小   最多生产产品的个数 当前池中的产品个数 生产了个产品数量  消费了的产品数量 */

mutex m_c, m_p, m;
/* 多消费者时的互斥量   多生产者时的互斥量  产品池的互斥量 */

condition_variable p_not_full, p_not_empty;
/* 生产者等待产品池不满   消费者等待产品池非空 */


void ProducerM ()
{
    while (true)
    {
        unique_lock<mutex> lck_p(m_p);
        if (producer_count == max_size) break;

        unique_lock<mutex> lck(m);
        while (pool_size == pool_max)
        {
            cout << "Pool full\t Wait..." << endl;
            p_not_full.wait(lck);
        }
        pool_size++;
        producer_count++;
        cout << "Producer : " << this_thread::get_id() << "\tgenerate : " << producer_count << endl;
        if (pool_size == 1)
        {
            cout << "Notify consumer...\n";
            p_not_empty.notify_one();
        }
        if (producer_count == max_size) break;
        lck.unlock();
        lck_p.unlock();
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
    cout << "Producer Single exit\t id is : " << this_thread::get_id() << endl;
}

void ConsumerM()
{
    while (true)
    {
        unique_lock<mutex> lck_c(m_c);
        if (consumer_count == max_size) break;

        unique_lock<mutex> lck(m);
        while (pool_size == 0)
        {
            cout << "Pool empty\t Wait...\n";
            p_not_empty.wait(lck);
        }
        pool_size--;
        consumer_count++;
        cout << "Consumer : " << this_thread::get_id() << "\tconsume : " << consumer_count << endl;
        if (pool_size == pool_max-1)
        {
            cout << "Notify producer...\n";
            p_not_full.notify_one();
        }
        if (consumer_count == max_size) break;
        lck.unlock();
        lck_c.unlock();
        this_thread::sleep_for(chrono::milliseconds(1500));
    }
    cout << "Consumer Single exit\t id is : " << this_thread::get_id() << endl;
}


void TestPmCm()
{
    const int p_t_i = 5, c_t_i = 7;
    thread t_c[c_t_i];
    thread t_p[p_t_i];
    for (int i=0; i<c_t_i; ++i)
        t_c[i] = thread(ConsumerM);
    for (int i = 0; i<p_t_i; ++i)
        t_p[i] = thread(ProducerM);

    for (int i=0; i<c_t_i; ++i)
        t_c[i].join();
    for (int i = 0; i<p_t_i; ++i)
        t_p[i].join();
}


int main(void)
{
    TestPmCm();
    return 0;
}

运行截图:

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值