C++多线程编程之如何解决多线程数据共享问题

发布时间:2021-10-18 11:09:17 作者:iii
来源:亿速云 阅读:210

这篇文章主要讲解了“C++多线程编程之如何解决多线程数据共享问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++多线程编程之如何解决多线程数据共享问题”吧!

通过容器创建多个线程

#include <vector> #include <iostream> #include <thread> void printTest(int num)  {     std::cout << "子线程:" << num << "启动" << std::endl;     std::cout << "子线程:" << num << "结束" << std::endl; } int main()  {     std::vector<std::thread* > test;     for (int i = 0; i < 10; i++)      {         test.push_back(new std::thread(printTest, i));     }     for (auto& pmove : test)     {         pmove->join();     }     std::cout << "主线程" << std::endl;     return 0; }

数据共享问题分析只读数据:稳定安全,不需要特殊处理,直接读即可

#include <vector> #include <iostream> #include <thread> std::vector<int> g_data={ 1,2,3 }; void printTest(int num)  {  std::cout << "子线程:" << num << "读操作" << std::endl;  for (auto pmove : g_data)   {  std::cout << pmove << std::endl;  } } int main()  {  std::vector<std::thread* > test;  for (int i = 0; i < 10; i++)   {  test.push_back(new std::thread(printTest, i));  }  for (auto& pmove : test)  {  pmove->join();  }  std::cout << "主线程" << std::endl;  return 0; }

有读有写:需要做特别处理(写只做写,读只做读操作,保持共享数据只有唯一操作),不然会引发崩溃

#include <list> #include <iostream> #include <thread> class SeaKing  { public:  void makeFriend()  {  for (int i = 0; i < 100000; i++)   {  std::cout << "增加一个" << std::endl;  mm.push_back(i);  }  }  void breakUp()   {  for (int i = 0; i < 100000; i++)   {  if (!mm.empty())   {  std::cout << "减少一个:"<<mm.front() << std::endl;  mm.pop_front();  }  else   {  std::cout << "已空" << std::endl;  }  }  } protected:  std::list<int> mm; }; int main()  {  SeaKing man;  std::thread t1(&SeaKing::makeFriend, &man);  std::thread t2(&SeaKing::breakUp, &man);  t1.join();  t2.join();  return 0; } //以上程序会异常退出

加锁的方式解决数据共享问题互斥量mutex:  互斥量可以理解为锁,他是一个mutex类的对象通过调用成员函数lock函数进行加锁通过调用成员函数unlock函数进行解锁

#include <list> #include <iostream> #include <thread> #include <mutex> //1.包含头文件 class SeaKing  { public:  void makeFriend()  {  for (int i = 0; i < 100000; i++)   {  m_mutex.lock();  std::cout << "增加一个" << std::endl;  mm.push_back(i);  m_mutex.unlock();  }  }  bool readInfo()   {  m_mutex.lock(); //2.加锁  if (!mm.empty())  {  std::cout << "减少一个:" << mm.front() << std::endl;  mm.pop_front();  m_mutex.unlock();  return true;  }  m_mutex.unlock();  return false;  }  void breakUp()   {  for (int i = 0; i < 100000; i++)  {  int result = readInfo();  if (result == false)   {  std::cout << "已空" << std::endl;  }  }  } protected:  std::list<int> mm;  std::mutex m_mutex; //创建互斥量对象 }; int main()  {  SeaKing man;  std::thread t1(&SeaKing::makeFriend, &man);  std::thread t2(&SeaKing::breakUp, &man);  t1.join();  t2.join();  return 0; }

注意:lock函数与unlock都是成对出现,如果lock了没有调用unlock会引发异常,abort终止程序通过lock_guard加锁。

#include <list> #include <iostream> #include <thread> #include <mutex> class SeaKing  { public:     void makeFriend()     {         std::lock_guard<std::mutex> sbguard(m_mutex);         for (int i = 0; i < 100000; i++)          {             std::cout << "增加一个" << std::endl;             mm.push_back(i);         }     }     bool readInfo()      {         std::lock_guard<std::mutex> sbguard(m_mutex);         if (!mm.empty())         {             std::cout << "减少一个:" << mm.front() << std::endl;             mm.pop_front();             return true;         }         return false;     }     void breakUp()      {         for (int i = 0; i < 100000; i++)         {             int result = readInfo();             if (result == false)              {                 std::cout << "已空" << std::endl;             }         }     } protected:     std::list<int> mm;     std::mutex m_mutex; }; int main()  {     SeaKing man;     std::thread t1(&SeaKing::makeFriend, &man);     std::thread t2(&SeaKing::breakUp, &man);     t1.join();     t2.join();     return 0; }

其实lock_guard  在构造函数中进行lock,在析构函数中进行unlock,本质上还是lock与unlock操作。

感谢各位的阅读,以上就是“C++多线程编程之如何解决多线程数据共享问题”的内容了,经过本文的学习后,相信大家对C++多线程编程之如何解决多线程数据共享问题这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 多线程编程指南
  2. Linux 多线程编程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:如何使用JS函数式编程Reduce与Map

下一篇:怎么设置环境变量

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》