生产者消费者模型
1、python
import threading
import queue
import time
def comsumer(q: queue.Queue):
while True:
item = q.get() # 阻塞
print('comsumer: ', item, threading.get_ident())
q.task_done() # queue中保存一个计数,put+1, task_done-1
time.sleep(1)
def producer(q: queue.Queue):
for i in range(10):
q.put(i)
q = queue.Queue(maxsize=5)
t1 = threading.Thread(
target=comsumer, args=(q,), daemon=True
) # deamon表示只要主线程结束了,子线程就结束
t2 = threading.Thread(target=comsumer, args=(q,), daemon=True)
t1.start()
t2.start()
producer(q=q)
q.join() # 阻塞到此处,直到self.unfinished_tasks为零
2、C++
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <queue>
#include <random>
#include <thread>
#include <vector>
const int MAX_QUEUE_SIZE = 5;
const int DT = 2000;
using timepoint = std::chrono::_V2::steady_clock::time_point;
class ProducerConsumer
{
public:
ProducerConsumer() : done(false), rng(std::random_device{}())
{
std::uniform_int_distribution<int> produce_noise(-5, 5);
for (int i = 0; i < 51; ++i)
{
bag.push(i * 100 + produce_noise(rng));
}
}
void start()
{
producerThread = std::thread(&ProducerConsumer::produce, this);
consumerThread = std::thread(&ProducerConsumer::consume, this);
}
void join()
{
if (producerThread.joinable())
{
producerThread.join();
}
if (consumerThread.joinable())
{
consumerThread.join();
}
}
private:
void produce()
{
std::uniform_int_distribution<int> produce_delay(1, 10); // 随机小时间
while (!bag.empty())
{
std::this_thread::sleep_for(std::chrono::milliseconds(produce_delay(rng))); // 模拟生产时间
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return queue.size() < MAX_QUEUE_SIZE; });
queue.push(bag.front());
bag.pop();
lock.unlock(); // 解锁以允许消费者获取锁
cv.notify_all(); // 通知消费者线程
}
// 生产结束后通知消费者
{
std::lock_guard<std::mutex> lock(mtx);
done = true;
}
cv.notify_all();
}
void consume()
{
std::uniform_int_distribution<int> produce_delay(10, 110); // 随机处理数据时间
while (true)
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return !queue.empty() || done; });
int item;
if (!queue.empty())
{
item = queue.front();
queue.pop();
lock.unlock(); // 解锁以允许生产者获取锁
cv.notify_all(); // 通知生产者线程
std::this_thread::sleep_for(std::chrono::milliseconds(produce_delay(rng))); // 模拟消费时间
}
if (done && queue.empty())
{
break;
}
timepoint now = std::chrono::steady_clock::now();
if (hasStart)
{
auto planTimePoint = lastTimePoint + std::chrono::milliseconds(item - lastItem);
auto dt = std::chrono::duration_cast<std::chrono::milliseconds>(planTimePoint - now).count();
std::this_thread::sleep_for(std::chrono::milliseconds(dt));
now = std::chrono::steady_clock::now();
std::cout << "plandt: " << item - lastItem
<< ", realdt: " << std::chrono::duration_cast<std::chrono::milliseconds>(now - lastTimePoint).count() << std::endl;
}
else
{
hasStart = true;
}
lastTimePoint = now;
lastItem = item;
}
}
std::thread producerThread;
std::thread consumerThread;
std::queue<int> bag;
std::queue<int> queue;
std::mutex mtx;
std::condition_variable cv;
bool done;
bool hasStart = false;
int lastItem;
std::mt19937 rng;
timepoint lastTimePoint;
};
int main()
{
ProducerConsumer pc;
pc.start();
pc.join();
return 0;
}
示例2:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
std::queue<int> q;
std::mutex mtx;
std::condition_variable cv_producer;
std::condition_variable cv_consumer;
const size_t max_buffer_size = 10;
bool done = false;
void producer() {
for (int i = 0; i < 100; ++i) { // 快速生产 100 个项目
std::unique_lock<std::mutex> lock(mtx);
cv_producer.wait(lock, [] { return q.size() < max_buffer_size; }); // 等待队列有空间
q.push(i);
std::cout << "Produced: " << i << std::endl;
cv_consumer.notify_all(); // 通知消费者
}
{
std::lock_guard<std::mutex> lock(mtx);
done = true;
}
cv_consumer.notify_all(); // 通知所有消费者退出
}
void consumer() {
while (true) {
std::unique_lock<std::mutex> lock(mtx);
cv_consumer.wait(lock, [] { return !q.empty() || done; }); // 等待条件变量
if (!q.empty()) {
int val = q.front();
q.pop();
std::cout << "Consumed: " << val << std::endl;
cv_producer.notify_one(); // 通知生产者有空闲空间
}
if (done && q.empty()) break; // 如果生产者完成工作并且队列为空,退出循环
lock.unlock(); // 解锁互斥锁以允许生产者继续生产
std::this_thread::sleep_for(std::chrono::seconds(1)); // 按固定时间周期消费
}
}
int main() {
std::thread prod(producer);
std::thread cons(consumer);
prod.join();
cons.join();
return 0;
}