分为四种情况:单生产者单消费者;单生产者多消费者;多生产者单消费者;多生产者多消费者。
单生产者单消费者:
#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;
}
运行截图: