乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C++之多線程(C++11 thread.h文件實現(xiàn)多線程)

       禁忌石 2017-05-30
      2016-06-14 21:30 6344人閱讀 評論(2) 收藏 舉報
       分類:

      轉載自:

      http://www.cnblogs.com/haippy/p/3235560.html

      http://www.cnblogs.com/lidabo/p/3908705.html

      與 C++11 多線程相關的頭文件
      C++11 新標準中引入了四個頭文件來支持多線程編程,他們分別是<atomic> ,<thread>,<mutex>,<condition_variable>和<future>。
      <atomic>:該頭文主要聲明了兩個類, std::atomic 和 std::atomic_flag,另外還聲明了一套 C 風格的原子類型和與 C 兼容的原子操作的函數(shù)。
      <thread>:該頭文件主要聲明了 std::thread 類,另外 std::this_thread 命名空間也在該頭文件中。
      <mutex>:該頭文件主要聲明了與互斥量(mutex)相關的類,包括 std::mutex 系列類,std::lock_guard, std::unique_lock, 以及其他的類型和函數(shù)。
      <condition_variable>:該頭文件主要聲明了與條件變量相關的類,包括 std::condition_variable 和 std::condition_variable_any。
      <future>:該頭文件主要聲明了 std::promise, std::package_task 兩個 Provider 類,以及 std::future 和 std::shared_future 兩個 Future 類,另外還有一些與之相關的類型和函數(shù),std::async() 函數(shù)就聲明在此頭文件中。
      demo1:thread "Hello world"

      1. #include <stdio.h>  
      2. #include <stdlib.h>  
      3. #include <iostream> // std::cout  
      4. #include <thread>   // std::thread  
      5. void thread_task() {  
      6.     std::cout << "hello thread" << std::endl;  
      7. }  
      8. int main(int argc, const char *argv[])  
      9. {  
      10.     std::thread t(thread_task);  
      11.     t.join();//和主線程協(xié)同  
      12.     return EXIT_SUCCESS;  
      13. }  
      demo2:一次啟動多個線程
      我們通常希望一次啟動多個線程,來并行工作。為此,我們可以創(chuàng)建線程組,而不是在先前的舉例中那樣創(chuàng)建一條線程。下面的例子中,主函數(shù)創(chuàng)建十條為一組的線程,并且等待這些線程完成他們的任務
      1. #include <stdio.h>  
      2. #include <stdlib.h>  
      3. #include <iostream> // std::cout  
      4. #include <thread>   // std::thread  
      5. int main() {  
      6.     std::thread t[num_threads];  
      7.     //Launch a group of threads 啟動一組線程  
      8.     for (int i = 0; i < num_threads; ++i) {  
      9.         t[i] = std::thread(call_from_thread);  
      10.     }  
      11.     std::cout << "Launched from the mainn";  
      12.     //Join the threads with the main thread  
      13.     for (int i = 0; i < num_threads; ++i) {  
      14.         t[i].join();  
      15.     }  
      16.     return 0;  
      17. }  

      記住,主函數(shù)也是一條線程,通常叫做主線程,所以上面的代碼實際上有11條線程在運行。在啟動這些線程組之后,線程組和主函數(shù)進行協(xié)同(join)之前,允許我們在主線程中做些其他的事情。
      demo3:在線程中使用帶有形參的函數(shù)
      1. #include <stdio.h>  
      2. #include <stdlib.h>  
      3. #include <iostream> // std::cout  
      4. #include <thread>   // std::thread  
      5. static const int num_threads = 10;  
      6. //This function will be called from a thread  
      7. void call_from_thread(int tid) {  
      8.     std::cout << "Launched by thread " << tid << std::endl;  
      9. }  
      10. int main() {  
      11.     std::thread t[num_threads];  
      12.     //Launch a group of threads  
      13.     for (int i = 0; i < num_threads; ++i) {  
      14.         t[i] = std::thread(call_from_thread, i);  
      15.     }  
      16.     std::cout << "Launched from the mainn";  
      17.     //Join the threads with the main thread  
      18.     for (int i = 0; i < num_threads; ++i) {  
      19.         t[i].join();  
      20.     }  
      21.     return 0;  
      22. }  
      運行結果:
      [plain] view plain copy
      1. Sol$ ./a.out  
      2.   
      3.   
      4. Launched by thread 0  
      5.   
      6.   
      7. Launched by thread 1  
      8.   
      9.   
      10. Launched by thread 2  
      11.   
      12.   
      13. Launched from the main  
      14.   
      15.   
      16. Launched by thread 3  
      17.   
      18.   
      19. Launched by thread 5  
      20.   
      21.   
      22. Launched by thread 6  
      23.   
      24.   
      25. Launched by thread 7  
      26.   
      27.   
      28. Launched by thread Launched by thread 4  
      29.   
      30.   
      31. 8L  
      32.   
      33.   
      34. aunched by thread 9  
      35.   
      36.   
      37. Sol$  
      能看到上面的結果中,程序一旦創(chuàng)建一條線程,其運行存在先后秩序不確定的現(xiàn)象。程序員的任務就是要確保這組線程在訪問公共數(shù)據(jù)時不要出現(xiàn)阻塞。最后幾行,所顯示的錯亂輸出,表明8號線程啟動的時候,4號線程還沒有完成在stdout上的寫操作。事實上假定在你自己的機器上運行上面的代碼,將會獲得全然不同的結果,甚至是會輸出些混亂的字符。原因在于,程序內的11條線程都在競爭性地使用stdout這個公共資源(案:Race Conditions)。
      要避免上面的問題,可以在代碼中使用攔截器(barriers),如std:mutex,以同步(synchronize)的方式來使得一群線程訪問公共資源,或者,如果可行的話,為線程們預留下私用的數(shù)據(jù)結構,避免使用公共資源。我們在以后的教學中,還會講到線程同步問題,包括使用原子操作類型(atomic types)和互斥體(mutex)。

      更多內容請參考:

      C++11 并發(fā)指南一(C++11 多線程初探)
      http://www.cnblogs.com/haippy/p/3235560.html
      C++11 并發(fā)指南二(std::thread 詳解)
      http://www.cnblogs.com/haippy/p/3236136.html
      C++11 并發(fā)指南三(std::mutex 詳解)
      http://www.cnblogs.com/haippy/p/3237213.html

      C++11 并發(fā)指南三(Lock 詳解)
      http://www.cnblogs.com/haippy/p/3346477.html

      C++11 并發(fā)指南四(<future> 詳解一 std::promise 介紹)
      http://www.cnblogs.com/haippy/p/3239248.html

      C++11 并發(fā)指南四(<future> 詳解二 std::packaged_task 介紹)
      http://www.cnblogs.com/haippy/p/3279565.html

      C++11 并發(fā)指南四(<future> 詳解三 std::future & std::shared_future)
      http://www.cnblogs.com/haippy/p/3280643.html

      C++11 并發(fā)指南五(std::condition_variable 詳解)
      http://www.cnblogs.com/haippy/p/3252041.html

      C++11 并發(fā)指南六(atomic 類型詳解一 atomic_flag 介紹)
      http://www.cnblogs.com/haippy/p/3252056.html

      C++11 并發(fā)指南六( <atomic> 類型詳解二 std::atomic )
      http://www.cnblogs.com/haippy/p/3301408.html

      C++11 并發(fā)指南六(atomic 類型詳解三 std::atomic (續(xù)))
      http://www.cnblogs.com/haippy/p/3304556.html

      C++11 并發(fā)指南六(atomic 類型詳解四 C 風格原子操作介紹)
      http://www.cnblogs.com/haippy/p/3306625.html

      C++11 并發(fā)指南七(C++11 內存模型一:介紹)
      http://www.cnblogs.com/haippy/p/3412858.html

      C++11 并發(fā)指南九(綜合運用: C++11 多線程下生產(chǎn)者消費者模型詳解)
      http://www.cnblogs.com/haippy/p/3252092.html

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多