• C++线程锁读写rtsp视频流


    void readRTSP(const std::string&& rtsp) {
        cv::VideoCapture cap;
       // assert(!rtsp.empty());
        auto cvname = std::this_thread::get_id();
        std::ostringstream oss;
        oss << cvname;
        auto strName = oss.str();
        std::cout << "thread id " << strName << "read: " << rtsp << std::endl;
        cap.open(rtsp);
        if (!cap.isOpened()) {
            std::cout << "open video failed";
            cap.release();
        }
        cv::Mat frame;
        for (;;) {
            cap >> frame;
            if (frame.empty())
            {
                std::cout << "thread-" << strName << " frame empty! " << std::endl;;
                continue; //进入下一轮循环
            }
            std::unique_lock<std::mutex> lock{ m_mutex };//枷锁
           // if (frame_que->size() < MAXQUESIZE) 
            while (frame_que->size() == MAXQUESIZE) 
            { 
                std::cout << "**********队列满*********" << std::endl;
                my_cond.wait(lock); 
            }
            std::cout << "+++++++压入队列+++++++" << std::endl;
            frame_que->push(frame); //线程被唤醒
            my_cond.notify_one();
            std::cout << "生产者唤醒消费者" << std::endl;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
       for (;;) 
        {
            std::unique_lock<std::mutex>lock{ m_mutex };//枷锁
            while (frame_que->empty())
            {
                std::cout << "**********队列空*********" << std::endl;
                my_cond.wait(lock);
            }
            while (!frame_que->empty()) {
                auto frame = frame_que->front();//获取队首元素
                auto res = detector.Run(frame, conf_thres, iou_thres);
                Demo(frame, res, class_names);
                frame_que->pop(); //删除队首,如果没有其他线程再操作这个frame_que
                my_cond.notify_one();
                std::cout << "消费者唤醒生产者" << std::endl;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    void readRTSP(const std::string&& rtsp) {
        cv::VideoCapture cap;
       // assert(!rtsp.empty());
        auto cvname = std::this_thread::get_id();
        std::ostringstream oss;
        oss << cvname;
        auto strName = oss.str();
        std::cout << "thread id " << strName << "read: " << rtsp << std::endl;
        cap.open(rtsp);
        if (!cap.isOpened()) {
            std::cout << "open video failed";
            cap.release();
        }
        cv::Mat frame;
        while (cap.read(frame)) {
           // cap >> frame;
            if (frame.empty())
            {
                std::cout << "thread-" << strName << " frame empty! " << std::endl;;
                continue; //进入下一轮循环
            }
           // std::unique_lock lock{ m_mutex };//枷锁
            if (frame_que->size() < MAXQUESIZE) {
          /*  while (frame_que->size() == MAXQUESIZE) 
            { 
                std::cout << "**********队列满*********" << std::endl;
                my_cond.wait(lock); 
            }*/
            std::cout << "+++++++压入队列+++++++" << std::endl;
            frame_que->push(frame); //线程被唤醒
            // my_cond.notify_one();
            //std::cout << "生产者唤醒消费者" << std::endl;
            }
            else
            {
                std::cout << "**********队列满*********" << std::endl;
               // my_cond.notify_one();
                //std::this_thread::sleep_for(std::chrono::milliseconds(2)); // nanoseconds(20)纳秒 microseconds(2)毫秒 milliseconds(2)秒
                //my_cond.wait(lock, [] {})
                continue;
            }
         }
        cap.release();
        cv::destroyAllWindows();
    
    }
    fun2():
        m_mutex.lock();
        while (!frame_que->empty()) {
            auto frame = frame_que->front();//获取队首元素
            auto res = detector.Run(frame, conf_thres, iou_thres);
            Demo(frame, res, class_names);
            frame_que->pop(); //删除队首,如果没有其他线程再操作这个frame_que
            m_mutex.unlock();
        }
        std::cout << "-------队列空------" << std::endl;
            //my_cond.notify_one();
        m_mutex.unlock();
     or fun2():
         for (;;) 
        {
             std::unique_lock<std::mutex>lock{ m_mutex };//枷锁
         /*   while (frame_que->empty())
            {
                std::cout << "**********队列空*********" << std::endl;
                my_cond.wait(lock);
            }*/
            m_mutex.lock();
            if (!frame_que->empty()) {
                auto frame = frame_que->front();//获取队首元素
                auto res = detector.Run(frame, conf_thres, iou_thres);
                Demo(frame, res, class_names);
                frame_que->pop(); //删除队首,如果没有其他线程再操作这个frame_que
                // my_cond.notify_one();
               // std::cout << "消费者唤醒生产者" << std::endl;
            }
            else
            {
                std::cout << "-------队列空------" << std::endl;
                //my_cond.notify_one();
                m_mutex.unlock();
                continue;
            }
            m_mutex.unlock();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    ES6的模块化管理、立即执行函数(IIFE):在函数声明后面立即调用、函数劫持
    5秒盾,加速乐
    数据库理论知识及相关发展方向
    计算机算法分析与设计(21)---回溯法(图着色问题)
    西南交通大学智能监测 培训课程练习2
    kali工具熟悉——网络扫描
    从零开始学定位 --- 使用kaist数据集进行LIO-SAM建图
    Go 复合类型之切片类型介绍
    【开源项目 - export-service】数据导出项目 之 简介
    从零开始学习JavaScript:轻松掌握编程语言的核心技能①
  • 原文地址:https://blog.csdn.net/z_6_2_0_s/article/details/128167959