当前位置: 首页 > article >正文

Leetcode—1279. 红绿灯路口【简单】Plus(多线程)

2024每日刷题(186)

Leetcode—1279. 红绿灯路口

在这里插入图片描述

C++实现代码

class TrafficLight {
public:
    TrafficLight() {
        
    }

    void carArrived(
        int carId,                   // ID of the car
        int roadId,                  // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
        int direction,               // Direction of the car
        function<void()> turnGreen,  // Use turnGreen() to turn light to green on current road
        function<void()> crossCar    // Use crossCar() to make car cross the intersection
    ) {
        lock_guard<std::mutex> loc(mtx);
        if(roadId != curRoadId) {
            curRoadId = roadId;
            turnGreen();
        }
        crossCar();
    }
private:
    mutex mtx;
    int curRoadId = 1;
};

运行结果

在这里插入图片描述
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!


http://www.kler.cn/news/357791.html

相关文章:

  • redis的发布订阅模式
  • vue3缓存菜单
  • Python从0到100(六十四):Python OpenCV-图像运算进阶实战
  • laravel-数据库查询
  • HarmonyNext保存Base64文件到Download下
  • 使用多路复用技术提升网站性能
  • 页面中包含多个el-popover,点击其中一个显示,其他的关闭(多个el-popover,click触发,点击都不消失的问题)
  • docker 基础镜像里 scratch 和alpine,ubuntu centos详细对比(镜像优化)
  • 如何声明一个类?类如何继承?
  • 网络最快的速度光速,因此‘‘光网络‘‘由此产生
  • Codeforces Round 979 (Div. 2) B. Minimise Oneness
  • WileyNJDv5_Template模板无法编译生成pdf文件
  • HTML+CSS (基础)
  • qt 序列化和反序列化
  • AI 代写是变现最快的副业项目,没有之一
  • docker harbor
  • Python学习的自我理解和想法(16)
  • 简单说说 spring构造器循环依赖 为什么无法解决(源码解析)
  • webpack 学习入门
  • Spring Boot技术:图书进销存管理的创新实践