<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++多線程編程(二)

          共 4359字,需瀏覽 9分鐘

           ·

          2021-03-03 14:25

          多線程是程序員必須掌握的一門技術(shù),本文主要是針對(duì)于C++新標(biāo)準(zhǔn)中多線程庫(kù),需要具備一定C++基礎(chǔ)方可學(xué)習(xí)。

          前言


          本章節(jié)是C++多線程編程第二課,C++不熟悉的可以轉(zhuǎn)接C++專輯教程,本章節(jié)主要講解C++多線程編程中容器創(chuàng)建線程以及數(shù)據(jù)共享問(wèn)題。

          容器創(chuàng)建管理線程


          #include <vector>#include <iostream>#include <thread>void printTest(int num) {  std::cout << "子線程:" << num << "啟動(dòng)" << std::endl;  std::cout << "子線程:" << num << "結(jié)束" << 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;}
          C++數(shù)據(jù)共享問(wèn)題分析


          1.只讀數(shù)據(jù):穩(wěn)定安全,不需要特殊處理,直接讀即可

          #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;}

          2.有讀有寫:需要做特別處理(寫只做寫,讀只做讀操作,保持共享數(shù)據(jù)只有唯一操作),不然會(huì)引發(fā)奔潰

          #include <list>#include <iostream>#include <thread>class SeaKing {public:  void makeFriend(){    for (int i = 0; i < 100000; i++)     {      std::cout << "增加一個(gè)" << std::endl;      mm.push_back(i);    }  }  void breakUp() {    for (int i = 0; i < 100000; i++)     {      if (!mm.empty())       {        std::cout << "減少一個(gè):"<<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;}//以上程序會(huì)異常退
          加鎖的方式解決數(shù)據(jù)共享問(wèn)題


          1.互斥量mutex: 互斥量可以理解為鎖,他是一個(gè)mutex類的對(duì)象

          • 通過(guò)調(diào)用成員函數(shù)lock函數(shù)進(jìn)行加鎖

          • 通過(guò)調(diào)用成員函數(shù)unlock函數(shù)進(jìn)行解鎖

          #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 << "增加一個(gè)" << std::endl;      mm.push_back(i);      m_mutex.unlock();    }  }  bool readInfo() {    m_mutex.lock();        //2.加鎖    if (!mm.empty())    {      std::cout << "減少一個(gè):" << 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;        //創(chuàng)建互斥量對(duì)象};int main() {  SeaKing man;  std::thread t1(&SeaKing::makeFriend, &man);  std::thread t2(&SeaKing::breakUp, &man);  t1.join();  t2.join();  return 0;}

          注意:lock函數(shù)與unlock都是成對(duì)出現(xiàn),如果lock了沒(méi)有調(diào)用unlock會(huì)引發(fā)異常,abort終止程序

          2.通過(guò)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 << "增加一個(gè)" << std::endl;      mm.push_back(i);    }  }  bool readInfo() {    std::lock_guard<std::mutex> sbguard(m_mutex);    if (!mm.empty())    {      std::cout << "減少一個(gè):" << 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;}

          其實(shí)lock_guard 在構(gòu)造函數(shù)中進(jìn)行l(wèi)ock,在析構(gòu)函數(shù)中進(jìn)行unlock,本質(zhì)上還是lock與unlock操作。

          好了,創(chuàng)建線程就介紹到這里,大家可以先練習(xí)一下,下章節(jié)講解共享數(shù)據(jù)訪問(wèn)。喜歡的不如點(diǎn)個(gè)“在看”吧



          瀏覽 32
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久久久久伊人 | 小黄片软件下载 | 九九九在线视频观看 | 日本成人激情视频 | 午夜精品视频成人精品视频 |