[鍘焆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>
- class thread
- {
- public:
- thread() noexcept;
- thread( thread&& other ) noexcept;
- template< class Function, class... Args >
- explicit thread( Function&& f, Args&&... args );
- thread(const thread&) = delete;
- ~thread();
- thread& operator=( thread&& other ) noexcept;
- bool joinable() const noexcept;
- std::thread::id get_id() const noexcept;
- native_handle_type native_handle();
- void join();
- void detach();
- void swap( thread& other ) noexcept;
- static unsigned int hardware_concurrency() noexcept;
- };
浠庡畾涔変腑鎴戜滑鍙互寰楃煡锛?/p>
- std::thread涓嶆敮鎸佹嫹璐濊涔夈€?/li>
- std::thread鏀寔绉诲姩璇箟銆?/li>
鍚勪釜鎴愬憳鍑芥暟鐨勭畝鍗曚粙缁?/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>
- void some_function() {}
- void some_other_function() {}
- int main()
- {
- std::thread t1(some_function); // 鏋勯€犱竴涓猼hread瀵硅薄t1
- std::thread t2 = std::move(t1); // 鎶妕1 move缁欏彟澶栦竴涓猼hread瀵硅薄t2锛宼1涓嶅啀绠$悊涔嬪墠鐨勭嚎绋嬩簡銆?/span>
- // 杩欏彞涓嶉渶瑕乻td::move()锛屼粠涓存椂鍙橀噺杩涜绉诲姩鏄嚜鍔ㄥ拰闅愬紡鐨勩€傝皟鐢ㄧ殑鏄痮perator=(std::thread&&)
- t1 = std::thread(some_other_function);
- std::thread t3;
- t3 = std::move(t2); // 鎶妕2 move缁檛3
- // 鎶妕3 move缁檛1锛岄潪娉曘€傚洜涓篳t1`宸茬粡鏈変簡涓€涓浉鍏崇殑绾跨▼锛屼細璋冪敤`std::terminate()`鏉ョ粓姝㈢▼搴忋€?/span>
- t1 = std::move(t3);
- }
- 閫氳繃璋冪敤join()鎴愬憳鍑芥暟鏉ョ瓑寰呯嚎绋嬬粨鏉?/li>
-
- void foo() {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
-
- int main() {
- std::cout << "starting first helper...
";
- std::thread helper1(foo);
- helper1.join();
- }
- 浼犻€掑弬鏁扮粰绾跨▼鍑芥暟
- void f(int i, std::string const& s);
- std::thread t(f, 3 "hello");
娉ㄦ剰锛氬弬鏁颁細浠ラ粯璁ょ殑鏂瑰紡琚鍒跺埌鍐呴儴瀛樺偍绌洪棿锛岀洿鍒颁娇鐢ㄧ殑鏃跺€欐墠浼氳浆鎴愬搴旂殑绫诲瀷銆?/p>
涓嬮潰鐨勪緥瀛愭湁闂鍚楋紵鏈変粈涔堥棶棰橈紵
- void f(int i, std::string const& s);
- void oops(int some_param)
- {
- char buffer[1024];
- sprintf(buffer, "%i", some_param);
- std::thread t(f, 3, buffer);
- 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>
- std::thread t(update_data_for_widget, w, std::ref(data));
- 浣跨敤绫荤殑鎴愬憳鍑芥暟浣滀负绾跨▼鍙傛暟
- class CRunner
- {
- public:
- void run0(){}
- void run1(int a) {}
- void run2(int a, int b) {}
- int run3(int a, char b, const std::string& c) {return 0;}
- int run4(int& a, double b, float c, char d) { ++a; return 0; }
- static void run_static(int a) {}
- };
-
- int main()
- {
- CRunner runner;
- int a = 0;
- // 浣跨敤std::mem_fun
- std::thread t0(std::mem_fun(&CRunner::run0), &runner);
- // 浣跨敤std::mem_fun_ref锛屾敞鎰忎笌std::mem_fun鐨勫尯鍒?/span>
- std::thread t1(std::mem_fun_ref(&CRunner::run1), std::ref(runner), 1);
- // 浣跨敤std::mem_fn锛宻td::mem_fn鏀寔澶氫簬涓€涓弬鏁扮殑鍑芥暟锛宻td::mem_fun涓嶆敮鎸併€?/span>
- std::thread t2(std::mem_fn(&CRunner::run2), std::ref(runner), 1, 2);
- // 浣跨敤std::bind + std::mem_fn
- std::thread t3(std::bind(std::mem_fn(&CRunner::run3), &runner, 1, 2, "data"));
- // 浣跨敤std::mem_fn锛屾敞鎰弒td::ref鐨勭敤娉曪紝濡傛灉涓嶇敤std::ref琛屼笉琛岋紵
- std::thread t4(std::mem_fn(&CRunner::run4), &runner, std::ref(a), 2.2, 3.3f, 鈥榙鈥?;
- // 浣跨敤绫荤殑闈欐€佸嚱鏁?/span>
- std::thread t5(&CRunner::run_static, 1);
-
- t0.join();
- t1.join();
- t2.join();
- t3.join();
- t4.join();
- t5.join();
- }
鍙傝€冭祫鏂?/h2>
- 銆奀++骞跺彂缂栫▼瀹炴垬銆?/li>
- cppreference
以上是关于[鍘焆std::thread鎬荤粨的主要内容,如果未能解决你的问题,请参考以下文章