C++ 多线程(互斥锁、条件变量)

互斥锁

最简单示例:

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;
int counter = 0;

void increment() {
    for (int i = 0; i < 10000; ++i) {
        // b1 是创建出来的对象.  lock_guard 类似智能指针一样,为了防止忘记释放锁
        // 锁的管理范围看作用域范围 {}
        std::lock_guard<std::mutex> b1(mtx);
        counter++;
    }
}

int main() {
    std::thread t1(increment);
    std::thread t2(increment);

    t1.join();
    t2.join();

    std::cout << "Counter value: " << counter << std::endl;

    return 0;
}

条件变量+互斥锁

互斥锁:保证只要一个线程在修改变量

条件变量:线程1与线程2之间需要一个同步信号,来统一控制线程是否该运行或者等待

cpp 复制代码
#include <iostream>
#include <string>
#include <thread>  // 多线程
#include <vector>
#include <mutex>  // 互斥锁
#include <condition_variable>  // 条件变量
#include <chrono>  // 处理时间库

//using namespace std;

std::mutex mtx1;              // 互斥锁对象   
std::condition_variable cv_1; // 条件变量对象

bool state = false;          // 条件变量 变量

// 不使用传递引用
//void read_camera(std::string rtsp, std::string window_name) {

// 使用传递引用避免拷贝
void read_camera(const std::string& rtsp, const std::string& window_name) {

    // 互斥锁 使用
    std::unique_lock<std::mutex> lck(mtx1);
    while (!state) {
        cv_1.wait(lck);  // 条件不成立 进入阻塞状态 释放掉锁 等待别人唤醒
    }
    
    for (int i = 0; i < 9999; i++) {
        std::cout << i << std::endl;
    }
    std::cout << "后启动的线程id:" << std::this_thread::get_id() << std::endl;
    std::cout << "rtsp" << rtsp << std::endl;
    std::cout << "window_name    " << window_name << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));  // 休眠2秒
    
}


void read_camera_1() {

    std::cout << "线程id:" << std::this_thread::get_id() << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));  // 休眠2秒

    // 互斥锁 使用
    std::unique_lock<std::mutex> lck(mtx1);
    state = true;
    cv_1.notify_all();  // 通知所有线程
}

int main()
{

     单个线程启动
    //std::string param1 = "./jian_qian.mp4";
    //std::string param2 = "1";

    //std::thread t1([param1, param2]() {
    //    read_camera(param1, param2);
    //    });
 
    // 测试互斥锁 休眠2秒
    std::thread t([ ]() {
        read_camera_1();
        });

    // 循环启动线程

    // 要保证容器内的数量一致
    std::vector <std::string> camera_path{ "./jian_qian.mp4", "./jian_qian.mp4" };
    std::vector <std::string> camera_name{ "1", "2" };
    // 这里我用循环启动了2个线程,实际执行时,先会执行休眠2秒的函数,然后这两个线程会抢占运行先后顺序,
    // 假如第一个抢到了,第二个要等第一个运行完毕后才能运行,因为代码中互斥锁+条件变量 是只有一个线程可以运行,其他需要等待。
    std::vector<std::thread> threads;
    for (int i = 0; i < camera_path.size(); i++) {

        // 不传递引用
        //threads.push_back(std::thread([camera_path_i = camera_path[i], camera_name_i = camera_name[i]]() {
        //    read_camera(camera_path_i, camera_name_i);
        //    }));
        
        // 使用 std::ref 传递引用,避免值拷贝
        threads.push_back(std::thread(read_camera, std::ref(camera_path[i]), std::ref(camera_name[i])));
    }
    
    t.join();
    // 阻塞等待所有线程完成
    for (auto& t : threads) {
        // 避免重复启动线程,防止线程意外,未正确释放资源
        if (t.joinable()) {
            t.join();
        }
    }
    return 0;
}
相关推荐
Java__攻城狮7 分钟前
java设计模式(持续更新中)
java·开发语言·设计模式
铁打的阿秀18 分钟前
okhttp 报java.lang.IllegalStateException: closed
java·开发语言·okhttp
Zhen (Evan) Wang24 分钟前
What is new in C# 7,8,9,10
开发语言·c#
桃酥40334 分钟前
贪心算法day31|56. 合并区间、738. 单调递增的数字(整数与字符串的转换)、贪心刷题总结
c++·算法·leetcode·贪心算法·字符串
有杨既安然34 分钟前
Python数据分析与可视化基础教程
开发语言·python·信息可视化·数据分析·excel
van叶~35 分钟前
移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——12.二叉树(习题)
开发语言·c++·算法
爱吃喵的鲤鱼36 分钟前
C++——深部解析哈希
c++·算法·哈希算法
`北极星42 分钟前
应用程序已被 Java 安全阻止:Java 安全中的添加的例外站点如何对所有用户生效
java·开发语言·安全
蒙娜丽宁1 小时前
深入解析Go语言的类型方法、接口与反射
java·开发语言·golang·go
会敲代码的小张1 小时前
设计模式-外观模式
java·开发语言·后端·设计模式·外观模式