当前位置:主页 > 查看内容

C++多线程编程之多线程数据共享问题

发布时间:2021-05-08 00:00| 位朋友查看

简介:通过容器创建多个线程 #includevector #includeiostream #includethread voidprintTest( int num) { std::cout 子线程: num 启动 std::endl; std::cout 子线程: num 结束 std::endl; } int main() { std::vectorstd::thread*test; for ( int i=0;i10;i++) {……

通过容器创建多个线程

  1. #include <vector> 
  2. #include <iostream> 
  3. #include <thread> 
  4. void printTest(int num)  
  5.     std::cout << "子线程:" << num << "启动" << std::endl; 
  6.     std::cout << "子线程:" << num << "结束" << std::endl; 
  7. int main()  
  8.     std::vector<std::thread* > test; 
  9.     for (int i = 0; i < 10; i++)  
  10.     { 
  11.         test.push_back(new std::thread(printTest, i)); 
  12.     } 
  13.     for (auto& pmove : test) 
  14.     { 
  15.         pmove->join(); 
  16.     } 
  17.     std::cout << "主线程" << std::endl; 
  18.     return 0; 

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

  1. #include <vector> 
  2. #include <iostream> 
  3. #include <thread> 
  4. std::vector<int> g_data={ 1,2,3 }; 
  5. void printTest(int num)  
  6.  std::cout << "子线程:" << num << "读操作" << std::endl; 
  7.  for (auto pmove : g_data)  
  8.  { 
  9.  std::cout << pmove << std::endl; 
  10.  } 
  11. int main()  
  12.  std::vector<std::thread* > test; 
  13.  for (int i = 0; i < 10; i++)  
  14.  { 
  15.  test.push_back(new std::thread(printTest, i)); 
  16.  } 
  17.  for (auto& pmove : test) 
  18.  { 
  19.  pmove->join(); 
  20.  } 
  21.  std::cout << "主线程" << std::endl; 
  22.  return 0; 

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

  1. #include <list> 
  2. #include <iostream> 
  3. #include <thread> 
  4. class SeaKing  
  5. public
  6.  void makeFriend() 
  7.  { 
  8.  for (int i = 0; i < 100000; i++)  
  9.  { 
  10.  std::cout << "增加一个" << std::endl; 
  11.  mm.push_back(i); 
  12.  } 
  13.  } 
  14.  void breakUp()  
  15.  { 
  16.  for (int i = 0; i < 100000; i++)  
  17.  { 
  18.  if (!mm.empty())  
  19.  { 
  20.  std::cout << "减少一个:"<<mm.front() << std::endl; 
  21.  mm.pop_front(); 
  22.  } 
  23.  else  
  24.  { 
  25.  std::cout << "已空" << std::endl; 
  26.  } 
  27.  } 
  28.  } 
  29. protected: 
  30.  std::list<int> mm; 
  31. }; 
  32. int main()  
  33.  SeaKing man; 
  34.  std::thread t1(&SeaKing::makeFriend, &man); 
  35.  std::thread t2(&SeaKing::breakUp, &man); 
  36.  t1.join(); 
  37.  t2.join(); 
  38.  return 0; 
  39. //以上程序会异常退出 

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

  1. #include <list> 
  2. #include <iostream> 
  3. #include <thread> 
  4. #include <mutex> //1.包含头文件 
  5. class SeaKing  
  6. public
  7.  void makeFriend() 
  8.  { 
  9.  for (int i = 0; i < 100000; i++)  
  10.  { 
  11.  m_mutex.lock(); 
  12.  std::cout << "增加一个" << std::endl; 
  13.  mm.push_back(i); 
  14.  m_mutex.unlock(); 
  15.  } 
  16.  } 
  17.  bool readInfo()  
  18.  { 
  19.  m_mutex.lock(); //2.加锁 
  20.  if (!mm.empty()) 
  21.  { 
  22.  std::cout << "减少一个:" << mm.front() << std::endl; 
  23.  mm.pop_front(); 
  24.  m_mutex.unlock(); 
  25.  return true
  26.  } 
  27.  m_mutex.unlock(); 
  28.  return false
  29.  } 
  30.  void breakUp()  
  31.  { 
  32.  for (int i = 0; i < 100000; i++) 
  33.  { 
  34.  int result = readInfo(); 
  35.  if (result == false)  
  36.  { 
  37.  std::cout << "已空" << std::endl; 
  38.  } 
  39.  } 
  40.  } 
  41. protected: 
  42.  std::list<int> mm; 
  43.  std::mutex m_mutex; //创建互斥量对象 
  44. }; 
  45. int main()  
  46.  SeaKing man; 
  47.  std::thread t1(&SeaKing::makeFriend, &man); 
  48.  std::thread t2(&SeaKing::breakUp, &man); 
  49.  t1.join(); 
  50.  t2.join(); 
  51.  return 0; 

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

  1. #include <list> 
  2. #include <iostream> 
  3. #include <thread> 
  4. #include <mutex> 
  5. class SeaKing  
  6. public
  7.     void makeFriend() 
  8.     { 
  9.         std::lock_guard<std::mutex> sbguard(m_mutex); 
  10.         for (int i = 0; i < 100000; i++)  
  11.         { 
  12.             std::cout << "增加一个" << std::endl; 
  13.             mm.push_back(i); 
  14.         } 
  15.     } 
  16.     bool readInfo()  
  17.     { 
  18.         std::lock_guard<std::mutex> sbguard(m_mutex); 
  19.         if (!mm.empty()) 
  20.         { 
  21.             std::cout << "减少一个:" << mm.front() << std::endl; 
  22.             mm.pop_front(); 
  23.             return true
  24.         } 
  25.         return false
  26.     } 
  27.     void breakUp()  
  28.     { 
  29.         for (int i = 0; i < 100000; i++) 
  30.         { 
  31.             int result = readInfo(); 
  32.             if (result == false)  
  33.             { 
  34.                 std::cout << "已空" << std::endl; 
  35.             } 
  36.         } 
  37.     } 
  38. protected: 
  39.     std::list<int> mm; 
  40.     std::mutex m_mutex; 
  41. }; 
  42. int main()  
  43.     SeaKing man; 
  44.     std::thread t1(&SeaKing::makeFriend, &man); 
  45.     std::thread t2(&SeaKing::breakUp, &man); 
  46.     t1.join(); 
  47.     t2.join(); 
  48.     return 0; 

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


本文转载自网络,原文链接:https://www.toutiao.com/i6935305392079536672/
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐