[鍘焆std::thread鎬荤粨

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[鍘焆std::thread鎬荤粨相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/play' title='play'>play   鎷疯礉   _id   color   microsoft   fir   join()   cpp   tor   

缂樿捣

浠嶤++11寮€濮嬫彁渚涗簡绾跨▼鐨勬敮鎸侊紝缁堜簬鍙互鏂逛究鐨勭紪鍐欒法骞冲彴鐨勭嚎绋嬩唬鐮佷簡銆傞櫎浜?a href="https://en.cppreference.com/w/cpp/thread/thread" title="thread" target="_blank" style="color: #0088cc; text-decoration: none;">std::thread绫伙紝杩樻彁渚涗簡璁稿鍏跺畠渚垮埄鍚屾鐨勬満鍒讹紝鏈瘒鎬荤粨鏄疌++11瀛︿範绗旇绯诲垪鐨勯绡囨€荤粨銆?/p>

std::thread

std::thread瀹氫箟鍦?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;"><thread>涓紝鎻愪緵浜嗘柟渚跨殑鍒涘缓绾跨▼鐨勫姛鑳姐€?/p>

绫诲畾涔?/h3>
  1. class thread 
  2. { 
  3. public
  4. thread() noexcept
  5. thread( thread&& other ) noexcept
  6. template< class Function, class... Args > 
  7. explicit thread( Function&& f, Args&&... args ); 
  8. thread(const thread&) = delete
  9. ~thread(); 
  10. thread& operator=( thread&& other ) noexcept
  11. bool joinable() const noexcept
  12. std::thread::id get_id() const noexcept
  13. native_handle_type native_handle()
  14. void join()
  15. void detach()
  16. void swap( thread& other ) noexcept
  17. static unsigned int hardware_concurrency() noexcept
  18. }; 

浠庡畾涔変腑鎴戜滑鍙互寰楃煡锛?/p>

鍚勪釜鎴愬憳鍑芥暟鐨勭畝鍗曚粙缁?/h3>
  • join() 鍙互鐢ㄦ潵绛夊緟绾跨▼缁撴潫锛屽彧鑳借皟鐢ㄤ竴娆°€?/li>
  • joinable()鏄惁涓庢煇涓湁鏁堢殑绾跨▼鍏宠仈銆?/li>
  • detach() 涓庡綋鍓嶇嚎绋嬪垎绂汇€?/li>
  • swap() 涓庡彟澶栦竴涓?a href="https://en.cppreference.com/w/cpp/thread/thread" title="thread" target="_blank" style="color: #0088cc; text-decoration: none;">std::thread浜ゆ崲銆?/li>
  • get_id()鑾峰彇id銆?/li>
  • native_handle() 杩斿洖骞冲彴鐩稿叧鐨勬暟鎹紝windows涓嬫槸HANDLE銆?/li>
  • hardware_concurrency() 杩斿洖鍙苟琛岃繍琛岀殑绾跨▼鏁伴噺锛屽彧鑳戒綔涓轰竴涓弬鑰冦€?/li>

渚嬪瓙

鍥犱负thread绫绘瘮杈冪畝鍗曪紝鎴戜滑閫氳繃鍑犱釜渚嬪瓙鏉ュ涔犮€?/p>

  • 鏀寔绉诲姩璇箟锛屼絾涓嶆敮鎸佹嫹璐濊涔?/li>
  1. #include <thread> 
  2. void some_function() {} 
  3. void some_other_function() {} 
  4. int main() 
  5. std::thread t1(some_function); // 鏋勯€犱竴涓猼hread瀵硅薄t1 
  6. std::thread t2 = std::move(t1); // 鎶妕1 move缁欏彟澶栦竴涓猼hread瀵硅薄t2锛宼1涓嶅啀绠$悊涔嬪墠鐨勭嚎绋嬩簡銆?/span> 
  7. // 杩欏彞涓嶉渶瑕乻td::move()锛屼粠涓存椂鍙橀噺杩涜绉诲姩鏄嚜鍔ㄥ拰闅愬紡鐨勩€傝皟鐢ㄧ殑鏄痮perator=(std::thread&&) 
  8. t1 = std::thread(some_other_function); 
  9. std::thread t3; 
  10. t3 = std::move(t2); // 鎶妕2 move缁檛3 
  11. // 鎶妕3 move缁檛1锛岄潪娉曘€傚洜涓篳t1`宸茬粡鏈変簡涓€涓浉鍏崇殑绾跨▼锛屼細璋冪敤`std::terminate()`鏉ョ粓姝㈢▼搴忋€?/span> 
  12. t1 = std::move(t3); 
  • 閫氳繃璋冪敤join()鎴愬憳鍑芥暟鏉ョ瓑寰呯嚎绋嬬粨鏉?/li>
  1. #include <iostream> 
  2. #include <thread> 
  3. #include <chrono> 
  4.  
  5. void foo()
  6. std::this_thread::sleep_for(std::chrono::seconds(1)); 
  7.  
  8. int main()
  9. std::cout << "starting first helper... "
  10. std::thread helper1(foo)
  11. helper1.join(); 
  • 浼犻€掑弬鏁扮粰绾跨▼鍑芥暟
  1. void f(int i, std::string const& s)
  2. std::thread t(f, 3 "hello")

娉ㄦ剰锛氬弬鏁颁細浠ラ粯璁ょ殑鏂瑰紡琚鍒跺埌鍐呴儴瀛樺偍绌洪棿锛岀洿鍒颁娇鐢ㄧ殑鏃跺€欐墠浼氳浆鎴愬搴旂殑绫诲瀷銆?/p>

涓嬮潰鐨勪緥瀛愭湁闂鍚楋紵鏈変粈涔堥棶棰橈紵

  1. void f(int i, std::string const& s)
  2. void oops(int some_param) 
  3. char buffer[1024]; 
  4. sprintf(buffer, "%i", some_param); 
  5. std::thread t(f, 3, buffer)
  6. t.detach(); 

灞€閮ㄥ彉閲?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">buffer鐨勬寚閽堜細琚紶閫掔粰鏂扮嚎绋嬶紝濡傛灉oops()鍦?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">buffer琚浆鎹㈡垚string涔嬪墠閫€鍑猴紝閭d箞浼氬鑷存湭瀹氫箟鐨勮涓恒€傝В鍐充箣閬撴槸鍦ㄦ瀯閫?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">std::thread鐨勬椂鍊欎紶閫?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">string鍙橀噺銆?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">std::thread t(f, 3, std::string(buffer));

鍥犱负榛樿浼氬鍒讹紝濡傛灉鍍忚浼犻€掑紩鐢紝闇€瑕佷娇鐢?code style="border: 0; border-radius: 4px; font-size: 90%; background-color: rgba(214 , 219 , 223 , 0.28); color: #2C3E50; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px;">std::ref()鏉ユ爣璇嗭紝灏卞儚std::bind()閭f牱銆?/p>

  1. std::thread t(update_data_for_widget, w, std::ref(data))
  • 浣跨敤绫荤殑鎴愬憳鍑芥暟浣滀负绾跨▼鍙傛暟
  1. #include <thread> 
  2. #include <string> 
  3. class CRunner 
  4. { 
  5. public
  6. void run0(){} 
  7. void run1(int a) {} 
  8. void run2(int a, int b) {} 
  9. int run3(int a, char b, const std::string& c) {return 0;} 
  10. int run4(int& a, double b, float c, char d) { ++a; return 0; } 
  11. static void run_static(int a) {} 
  12. }; 
  13.  
  14. int main() 
  15. CRunner runner; 
  16. int a = 0
  17. // 浣跨敤std::mem_fun 
  18. std::thread t0(std::mem_fun(&CRunner::run0), &runner)
  19. // 浣跨敤std::mem_fun_ref锛屾敞鎰忎笌std::mem_fun鐨勫尯鍒?/span> 
  20. std::thread t1(std::mem_fun_ref(&CRunner::run1), std::ref(runner), 1)
  21. // 浣跨敤std::mem_fn锛宻td::mem_fn鏀寔澶氫簬涓€涓弬鏁扮殑鍑芥暟锛宻td::mem_fun涓嶆敮鎸併€?/span> 
  22. std::thread t2(std::mem_fn(&CRunner::run2), std::ref(runner), 1, 2)
  23. // 浣跨敤std::bind + std::mem_fn 
  24. std::thread t3(std::bind(std::mem_fn(&CRunner::run3), &runner, 1, 2, "data"));  
  25. // 浣跨敤std::mem_fn锛屾敞鎰弒td::ref鐨勭敤娉曪紝濡傛灉涓嶇敤std::ref琛屼笉琛岋紵 
  26. std::thread t4(std::mem_fn(&CRunner::run4), &runner, std::ref(a), 2.2, 3.3f, 鈥榙鈥?;  
  27. // 浣跨敤绫荤殑闈欐€佸嚱鏁?/span> 
  28. std::thread t5(&CRunner::run_static, 1)
  29.  
  30. t0.join(); 
  31. t1.join(); 
  32. t2.join(); 
  33. t3.join(); 
  34. t4.join(); 
  35. t5.join(); 

鍙傝€冭祫鏂?/h2>

以上是关于[鍘焆std::thread鎬荤粨的主要内容,如果未能解决你的问题,请参考以下文章

20191324銆婁俊鎭畨鍏ㄤ笓涓氬璁恒€嬬浜屽懆瀛︿範鎬荤粨

琛ㄦ牸鎬荤粨

SQL璇彞鎬荤粨

EOS鍏辫瘑鎬荤粨

JQuery甯哥敤鏂规硶鎬荤粨

html5 瀛︿範鎬荤粨