多线程的通信和产线问题 mutex, condition_variable

目标

目前有5个生产步骤, 1 半导体, 2 芯片, 3 外壳, 4包装, 5销售

如何写一段代码, 使得生产线可以按顺序生产

代码


#include <iostream>  
#include <thread>  
#include <mutex>  
#include <condition_variable>  
  
// 定义产线编号  
enum class ProductionLine { Semiconductor, Chip, Case, Shipping, Sales };  
  
// 定义共享资源  
std::mutex mtx;  
std::condition_variable cv;  
int semiconductors = 0;  
int chips = 0;  
int cases = 0;  
bool readyToShip = false;  
  
// 生产线函数  
void produce(ProductionLine line) {  
    while (true) {  
      //   std::cout << "1是" << semiconductors << "-" << "2是" << chips << "-" << "3是" << cases << std::endl;  

        std::unique_lock<std::mutex> lock(mtx);  
        cv.wait(lock, [line]() {
            switch (line) {  
                case ProductionLine::Semiconductor:  
                    return semiconductors == 0;  
                case ProductionLine::Chip:  
                    return semiconductors > 0 && chips == 0;  
                case ProductionLine::Case:  
                    return semiconductors > 0 && chips > 0 && cases == 0;  
                default:  
                    return false;  
            }  
        });  
  
        // 生产  
        switch (line) {  
            case ProductionLine::Semiconductor:  
                std::cout << "Producing semiconductors." << std::endl;  
                semiconductors++;  
                break;  
            case ProductionLine::Chip:  
                std::cout << "Producing chips." << std::endl;  
                chips++;  
                break;  
            case ProductionLine::Case:  
                std::cout << "Producing cases." << std::endl;  
                cases++;  
                break;  
            default:  
                break;  
        }  
  
        // 检查是否所有部件都已生产完毕  
        if (semiconductors > 0 && chips > 0 && cases > 0) {  
            std::cout << "生产完毕" << std:: endl;
            readyToShip = true;  
            
        }  
        cv.notify_all();  
        // 解锁互斥锁,让其他线程可以继续运行  
        lock.unlock();  
    }  
}  
  
// 发货和销售函数  
void shipAndSell() {  
    while (true) {  
        std::cout << "进入销售" << std:: endl;
        std::unique_lock<std::mutex> lock(mtx);  
        cv.wait(lock, []() { return readyToShip; });  
  
        // 发货  
        std::cout << "Shipping the product." << std::endl;  
        readyToShip = false;  
        semiconductors--;  
        chips--;  
        cases--;  
  
        // 通知其他线程可以继续生产  
        cv.notify_all();  
  
        // 解锁互斥锁,让其他线程可以继续运行  
        lock.unlock();  
  
        // 假设销售是一个简单的打印消息  
        std::cout << "Product sold." << std::endl;  
    }  
}  
  
int main() {  
    // 创建生产线程  
    std::thread producer1(produce, ProductionLine::Semiconductor);  
    std::thread producer2(produce, ProductionLine::Chip);  
    std::thread producer3(produce, ProductionLine::Case);  
    // 创建发货和销售线程  
    std::thread shipperAndSeller(shipAndSell);  
  
    // 等待所有线程完成  
    producer1.join();  
    producer2.join();  
    producer3.join();  
    shipperAndSeller.join();  
  
    return 0;  
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东哥爱编程

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值