1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      總結(jié)c++11&14-多線程要點(diǎn)

      這篇文章主要總結(jié)c++11&14-多線程要點(diǎn),內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

      在成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)過程中,需要針對(duì)客戶的行業(yè)特點(diǎn)、產(chǎn)品特性、目標(biāo)受眾和市場(chǎng)情況進(jìn)行定位分析,以確定網(wǎng)站的風(fēng)格、色彩、版式、交互等方面的設(shè)計(jì)方向。創(chuàng)新互聯(lián)建站還需要根據(jù)客戶的需求進(jìn)行功能模塊的開發(fā)和設(shè)計(jì),包括內(nèi)容管理、前臺(tái)展示、用戶權(quán)限管理、數(shù)據(jù)統(tǒng)計(jì)和安全保護(hù)等功能。

      在C++11以前,C++的多線程編程均需依賴系統(tǒng)或第三方接口實(shí)現(xiàn),一定程度上影響了代碼的移植性。C++11中,引入了boost庫中的多線程部分內(nèi)容,形成C++標(biāo)準(zhǔn),形成標(biāo)準(zhǔn)后的boost多線程編程部分接口基本沒有變化,這樣方便了以前使用boost接口開發(fā)的使用者切換使用C++標(biāo)準(zhǔn)接口,很容易把boost接口升級(jí)為C++標(biāo)準(zhǔn)接口。

      我們通過如下幾部分介紹C++11多線程方面的接口及使用方法。

      1. std::thread

      std::thread為C++11的線程類,使用方法和boost接口一樣,非常方便,同時(shí),C++11的std::thread解決了boost::thread中構(gòu)成參數(shù)限制的問題,我想這都是得益于C++11的可變參數(shù)的設(shè)計(jì)風(fēng)格。

      我們通過如下代碼熟悉下std::thread使用風(fēng)格:

      //c11.cpp
      #include 
      #include 
      void threadfun1()
      {
       std::cout << "threadfun1 - 1\r\n" << std::endl;
       std::this_thread::sleep_for(std::chrono::seconds(1));
       std::cout << "threadfun1 - 2" << std::endl;
      }
      void threadfun2(int iParam, std::string sParam)
      {
       std::cout << "threadfun2 - 1" << std::endl;
       std::this_thread::sleep_for(std::chrono::seconds(5));
       std::cout << "threadfun2 - 2" << std::endl;
      }
      int main()
      {
       std::thread t1(threadfun1);
       std::thread t2(threadfun2, 10, "abc");
       t1.join();
       std::cout << "join" << std::endl;
       t2.detach();
       std::cout << "detach" << std::endl;
      }

      注意編譯時(shí)要使用:g++ c11.cpp -lpthread

      運(yùn)行結(jié)果:

      threadfun1 - 1
      threadfun2 - 1
      threadfun1 - 2
      join
      detach

      2. std::atomic

      std::atomic為C++11封裝的原子數(shù)據(jù)類型。
      什么是原子數(shù)據(jù)類型?從功能上看,簡(jiǎn)單地說,原子數(shù)據(jù)類型不會(huì)發(fā)生數(shù)據(jù)競(jìng)爭(zhēng),能直接用在多線程中而不必我們用戶對(duì)其進(jìn)行添加互斥資源鎖的類型。從實(shí)現(xiàn)上來看,我們可以理解為這些原子類型內(nèi)部自己加了鎖。

      我們下面通過一個(gè)測(cè)試?yán)诱f明原子類型std::atomic的特點(diǎn)。

      我們使用10個(gè)線程,把std::atomic類型的變量iCount從10減到1。

      //c11.cpp
      #include 
      #include 
      #include 
      #include 
      #include 
      std::atomic bIsReady(false);
      std::atomic iCount(10);
      void threadfun1()
      {
       if (!bIsReady) {
        std::this_thread::yield();
       }
       while (iCount > 0)
       {
        printf("iCount:%d\r\n", iCount--);
       }
      }
      int main()
      {
       std::list lstThread;
       for (int i = 0; i < 10; ++i)
       {
        lstThread.push_back(std::thread(threadfun1));
       }
       for (auto& th : lstThread)
       {
        th.join();
       }
      }

      運(yùn)行結(jié)果:

      iCount:10
      iCount:9
      iCount:8
      iCount:7
      iCount:6
      iCount:5
      iCount:4
      iCount:3
      iCount:2
      iCount:1

      從上面的結(jié)果可以看到,iCount的最小結(jié)果是1,沒有出現(xiàn)小于等于0的情況,大家可以把iCount改成100甚至1000看看,可能會(huì)更直觀一點(diǎn)。

      3. std::condition_variable

      C++11中的std::condition_variable就像Linux下使用pthread_cond_wait和pthread_cond_signal一樣,可以讓線程休眠,直到被喚醒,然后再重新執(zhí)行。線程等待在多線程編程中使用非常頻繁,經(jīng)常需要等待一些異步執(zhí)行的條件的返回結(jié)果。

      代碼如下:

      #include  // std::cout
      #include  // std::thread
      #include  // std::mutex, std::unique_lock
      #include  // std::condition_variable
      std::mutex mtx;
      std::condition_variable cv;
      bool ready = false;
      void print_id(int id) {
       std::unique_lock lck(mtx);
       while (!ready) cv.wait(lck); //線程將進(jìn)入休眠
       // ...
       std::cout << "thread " << id << '\n';
      }
      void go() {
       std::unique_lock lck(mtx);
       ready = true;
       cv.notify_all();
      }
      int main()
      {
       std::thread threads[10];
       // spawn 10 threads:
       for (int i = 0; i<10; ++i)
        threads[i] = std::thread(print_id, i);
       std::cout << "10 threads ready to race...\n";
       go(); // go!
       for (auto& th : threads) th.join();
       return 0;
      }

      運(yùn)行結(jié)果:

      10 threads ready to race...
      thread 0
      thread 1
      thread 2
      thread 3
      thread 4
      thread 5
      thread 6
      thread 7
      thread 8
      thread 9

      上面的代碼,在調(diào)用go函數(shù)之前,10個(gè)線程都處于休眠狀態(tài),當(dāng)cv.notify_all()運(yùn)行后,線程休眠結(jié)束,繼續(xù)往下運(yùn)行,最終輸出如上結(jié)果。

      以上就是關(guān)于總結(jié)c++11&14-多線程要點(diǎn)的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。


      當(dāng)前題目:總結(jié)c++11&14-多線程要點(diǎn)
      地址分享:http://www.ef60e0e.cn/article/jijphj.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        巫溪县| 深泽县| 和政县| 温泉县| 姜堰市| 全椒县| 沈丘县| 罗平县| 左权县| 贵德县| 平度市| 政和县| 易门县| 嘉义县| 浦东新区| 铁岭县| 洛阳市| 嘉祥县| 伊金霍洛旗| 高陵县| 大理市| 桂东县| 桃江县| 郯城县| 西昌市| 古蔺县| 葵青区| 邢台县| 兴国县| 青神县| 锡林郭勒盟| 石嘴山市| 新平| 高雄市| 闻喜县| 沙坪坝区| 洛川县| 延川县| 吉首市| 甘南县| 萨嘎县|