C++并发 std::thread
生活随笔
收集整理的這篇文章主要介紹了
C++并发 std::thread
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1. 無(wú)參
- 2. 參數(shù)傳入
- 3. 輸入輸出參數(shù)
- 4. lambda表達(dá)式
- 5. 類的成員函數(shù)
1. 無(wú)參
#include <iostream> #include <thread>void hello() {std::cout << "Hello Concurrent, thread id: " << std::this_thread::get_id() << std::endl; }int main() {std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;std::thread t(hello);t.join();return 0; }運(yùn)行結(jié)果:
- 包含頭文件 #include <thread> ;
- 創(chuàng)建 std::thread 的實(shí)例,傳入hello函數(shù),子線程執(zhí)行hello函數(shù);
- join 等待子線程完成。 或使用 detach 分離子線程與主線程,使主線程無(wú)需等待子線程完成。
2. 參數(shù)傳入
#include <iostream> #include <thread> #include <string>void hello_input(std::string str) {std::cout << str << ", hello Concurrent, thread id: " << std::this_thread::get_id() << std::endl; }int main() {std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;std::thread t2(hello_input, "test");t2.join();return 0; }運(yùn)行結(jié)果:
參數(shù)作為 std::thread 對(duì)象的第二個(gè)參數(shù)傳入
3. 輸入輸出參數(shù)
#include <iostream> #include <thread> #include <string>void hello_input_output(int a, int b, int& ret) {ret = a + b;std::cout << "a + b: " << ret << ", hello Concurrent, thread id: " << std::this_thread::get_id() << std::endl; }int main() {std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;int ret = 0;std::thread t3(hello_input_output, 1, 5, std::ref(ret));t3.join();std::cout << "hello_input_output, ret: " << ret << std::endl;return 0; }運(yùn)行結(jié)果:
這里是使用參數(shù)引用來(lái)作為輸出參數(shù), 使用的時(shí)候, std::thread 對(duì)象中傳入的輸出參數(shù)需要使用 std::ref() 函數(shù)加以包裝。
線程庫(kù)的內(nèi)部代碼會(huì)把參數(shù)的副本當(dāng)成move-only型別(只能移動(dòng),不可復(fù)制),這些副本被當(dāng)成臨時(shí)變量,以右值形式傳給新線程上的函數(shù)。最終,hello_input_output() 函數(shù)調(diào)用會(huì)收到一個(gè)右值作為參數(shù)。如果不加 std::ref() 這段代碼會(huì)編譯失敗。
加 std::ref() 后, hello_input_output() 函數(shù)收到的是ret變量的引用,而不是臨時(shí)副本。
4. lambda表達(dá)式
#include <iostream> #include <thread>int main() {std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;std::thread t4([] {std::cout << "lambda, thread id: " << std::this_thread::get_id() << std::endl;});t4.join();return 0; }運(yùn)行結(jié)果:
5. 類的成員函數(shù)
#include <iostream> #include <thread> #include <string>class MyClass { public:void do_some_thing(std::string str) {m_strstr = str;std::cout << this << " " << str << ", class member function, thread id: " << std::this_thread::get_id() << std::endl;};std::string getStrr() const { return m_strstr; };private:std::string m_strstr;};int main() {std::cout << "Main thread, thread id: " << std::this_thread::get_id() << std::endl;MyClass myClass;std::thread t5(&MyClass::do_some_thing, &myClass, "test");t5.join();std::cout << &myClass << " " << "myclass.getStrr: " << myClass.getStrr() << std::endl;return 0; }運(yùn)行結(jié)果:
若要將某個(gè)類的成員函數(shù)設(shè)定為線程函數(shù):
- 第一個(gè)參數(shù),傳入一個(gè)函數(shù)指針,指向該成員函數(shù);
- 第二個(gè)參數(shù),對(duì)象指針&myClass,或者std::ref(myClass)。 如果寫成myClass,不帶&或者std::ref,則創(chuàng)建線程的過(guò)程中,myClass被拷貝了一份;
- 第三個(gè)及以后的參數(shù),為調(diào)用的成員函數(shù)的參數(shù)。
總結(jié)
以上是生活随笔為你收集整理的C++并发 std::thread的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php页面背景url不显示图片,back
- 下一篇: 【C++】数列求和-加强版