C++多线程中的锁和条件变量使用教程分享!

在做多线程编程时,有两个场景我们都会遇到:

  1. 多线程访问共享资源,需要用到锁;
  2. 多线程间的状态同步,这个可用的机制很多,条件变量是广泛使用的一种。

今天我用一个简单的例子来给大家介绍下锁和条件变量的使用。

代码使用C++11

示例代码

  #include <iostream>  #include <mutex>  #include <thread>  #include <condition_variable>  std::mutex       g_mutex;   // 用到的全局锁  std::condition_variable g_cond;   // 用到的条件变量  int g_i    = 0;  bool g_running = true;  void ThreadFunc(int n) {       // 线程执行函数   for (int i = 0; i < n; ++i) {    {     std::lock_guard<std::mutex> lock(g_mutex);   // 加锁,离开{}作用域后锁释放     ++g_i;     std::cout << "plus g_i by func thread " << std::this_thread::get_id() << std::endl;    }   }   std::unique_lock<std::mutex> lock(g_mutex);    // 加锁   while (g_running) {    std::cout << "wait for exit" << std::endl;    g_cond.wait(lock);                // wait调用后,会先释放锁,之后进入等待状态;当其它进程调用通知激活后,会再次加锁   }   std::cout << "func thread exit" << std::endl;  }  int main() {   int     n = 100;   std::thread t1(ThreadFunc, n);    // 创建t1线程(func thread),t1会执行`ThreadFunc`中的指令   for (int i = 0; i < n; ++i) {    {     std::lock_guard<std::mutex> lock(g_mutex);     ++g_i;     std::cout << "plus g_i by main thread " << std::this_thread::get_id() << std::endl;    }   }   {    std::lock_guard<std::mutex> lock(g_mutex);    g_running = false;    g_cond.notify_one();   // 通知其它线程   }   t1.join();     // 等待线程t1结束   std::cout << "g_i = " << g_i << std::endl;  }

程序运行后,关键输出如下:

  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by func thread 139921006847744  plus g_i by func thread 139921006847744  plus g_i by func thread 139921006847744  plus g_i by func thread 139921006847744  plus g_i by func thread 139921006847744  wait for exit               // func thread等待main thread发来的退出信号  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  plus g_i by main thread 139921025066816  func thread exit  g_i = 200     // 锁机制保证了g_i的正确

可以看到:

  std::this_thread::get_id()  g_i

加锁方法介绍

加锁相关的代码为:

  {   std::lock_guard<std::mutex> lock(g_mutex);   ......  }

要点为:

这样就实现了加锁和解锁的过程,为什么不直接调用加锁解锁方法呢?

我想,这是因为如果加锁和解锁中间的代码出现了问题,导致线程函数异常退出,那么这个锁就一直无法得到释放,其它线程处理的不好的话,就会造成死锁了。

条件变量使用介绍

结束语

以上所述是小编给大家介绍的C++多线程中的锁和条件变量使用教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对<计算机技术网(www.ctvol.com)!!>网站的支持!

—-想了解C++多线程中的锁和条件变量使用教程分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/482574.html

(0)
上一篇 2020年11月9日
下一篇 2020年11月9日

精彩推荐