• C++ 多线程


    目录

    目录

    多线程的创建与执行

    多线程的互斥

    1. 创建一个mutex对象,lock(),unlock()

    2. 借助lock_guard

    3. unique_lock: lock_guard的升级加强版


    多线程的创建与执行

    1. C++借助标准库来实现。将函数名放入线程中即可,如果函数有参数,一同放入。

    2. 执行方式有两种

    join方式:当启动的线程执行完毕后,才会继续运行下面的代码

    detach方式:不必等待。

    1. void func(int n, char c) {
    2. for (int i = 0; i < n; i++) {
    3. cout << c;
    4. }
    5. cout << endl;
    6. }
    7. void test() {
    8. int n = 10;
    9. char c = '*';
    10. thread thread1(func, n, c); //创建线程
    11. c = '%';
    12. thread thread2(func, n, c);
    13. thread1.join(); //等待线程1执行结束才会执行线程2
    14. thread2.join();
    15. }

    多线程的互斥

    C++引入了一个标准库<mutex>帮助实现简单的线程互斥。其中有3种方法。

    1. 创建一个mutex对象,lock(),unlock()

    该对象对临界区上锁解锁,实现只有一个线程访问临界区。

    1. #include
    2. #include
    3. using namespace std;
    4. mutex mtx; //创建一个mutex对象mtx
    5. void func(int n, char c) {
    6. mtx.lock(); //对临界区上锁
    7. for (int i = 0; i < n; i++) {
    8. cout << c;
    9. }
    10. cout << endl;
    11. mtx.unlock(); //解锁
    12. }
    13. void test() {
    14. int n = 10;
    15. char c = '*';
    16. thread thread1(func, n, c); //创建线程
    17. c = '%';
    18. thread thread2(func, n, c);
    19. thread1.join(); //等待线程执行结束
    20. thread2.join();
    21. }

    2. 借助lock_guard

    创建一个lock_guard对象,他获取提供给他的互斥锁的所有权。

    lock_guard:创建即上锁,超出作用域自动解锁;不能中途解锁;不能复制(因为它的赋值拷贝设置为私有成员函数了)。

    优点:防止忘记解锁,造成死锁。

    缺点:整个作用域加锁,锁的粒度太大。可以自己再加一个大括号来控制作用域。

    1. #include
    2. #include
    3. int temp = 0;
    4. mutex temp_guard; //用来保护temp
    5. void func2(){
    6. //保证当当前进程访问调用函数fun2时,其他的会被阻塞
    7. //lock_guard在出作用域时会自动解锁
    8. //为访问公共资源 上锁
    9. const std::lock_guardlock(temp_guard);
    10. temp++;
    11. }
    12. void test02() {
    13. thread thread1(func2);
    14. thread thread2(func2);
    15. thread1.join();
    16. thread2.join();
    17. }

    3. unique_lock: lock_guard的升级加强版

    unique_lock的特点:

    1. 创建时可以不加锁(参数std::defer_lock),默认是创建加锁的。

    2. 可以随时加锁解锁。lock(),unlock()

    3. 不手动解锁的话,超出作用域就自动解锁了。

    4. 不能复制,可以移动

    1. struct Box //结构体中有一个数字 和mutex对象
    2. {
    3. std::mutex mtx; //有一个互斥量
    4. int num;
    5. Box(int n):num(n){}
    6. };
    7. void transfer(Box& from, Box& to, int n) {
    8. //先创建锁,稍后加锁
    9. std::unique_locklock1(from.mtx, std::defer_lock);
    10. std::unique_locklock2(to.mtx, std::defer_lock);
    11. //同时对他们加锁
    12. lock(lock1, lock2);
    13. from.num -= n;
    14. to.num += n;
    15. }
    16. void test03() {
    17. Box from(100);
    18. Box to(30);
    19. std::thread thread1(transfer, std::ref(from), std::ref(to), 10);
    20. std::thread thread2(transfer, std::ref(from), std::ref(to), 20);
    21. thread1.join();
    22. thread2.join();
    23. cout << from.num << " " << to.num << endl;
    24. cout << from.num << " " << to.num << endl;
    25. }

  • 相关阅读:
    GO 语言的并发模式你了解多少?
    kubeneters 1.20 二进制方式高可用部署
    Springboot 是这样提高创建 docker 容器的效率的
    linux安装aerospike免安装版
    CH1-模型训练优化
    spring基本使用
    【学习挑战赛】经典算法之折半查找
    Rust的结构体类型的使用
    前端面试--贡献给刚毕业的你们
    6.29面试题记录收集
  • 原文地址:https://blog.csdn.net/zhangyixing_mj/article/details/132816076