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;
    }
}
   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;
        }
    }
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<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;
        }
        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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值