C++11/14中線程如何調(diào)用類對象和線程傳參

這篇文章將為大家詳細(xì)講解有關(guān)C++11/14中線程如何調(diào)用類對象和線程傳參,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站專注于西盟網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供西盟營銷型網(wǎng)站建設(shè),西盟網(wǎng)站制作、西盟網(wǎng)頁設(shè)計(jì)、西盟網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造西盟網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供西盟網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

線程調(diào)用類對象

在前面的示例中,我們?yōu)榫€程任務(wù)使用了通常的函數(shù)。實(shí)際上,我們可以使用任何可調(diào)用對象或者lambda函數(shù),如下調(diào)用類對象的例子:

#include <iostream>
#include <thread>

class MyFunctor
{
public:
  void operator()() 
  {
    std::cout << "functor\n"; 
  }
};

int main()
{
  MyFunctor fnctor;
  std::thread t(fnctor);
  std::cout << "main thread\n";
  t.join();
  return 0;
}

 在這里,我們創(chuàng)建了一個(gè)函數(shù)對象,并將其分配給線程任務(wù),我們可能會用這種方法嘗試此實(shí)例:

// MyFunctor fnctor;
std::thread t(MyFunctor());

但是編譯不通過,所以,如果我們想讓它工作,我們應(yīng)該這樣做:

// MyFunctor fnctor;
std::thread t((MyFunctor()));

就是說我們必須添加 ()包含 MyFunctor().

為什么?我這邊不去深究,它跟C++函數(shù)聲明規(guī)定相關(guān)。

線程傳參

下面是將參數(shù)傳遞給線程的示例。 在這個(gè)例子中,我們傳遞一個(gè)字符串:

#include <iostream>
#include <thread>
#include <string>

void thread_function(std::string s)
{
  std::cout << "thread function ";
  std::cout << "message is = " << s << std::endl;
}

int main()
{
  std::string s = "Kathy Perry";
  std::thread t(&thread_function, s);
  std::cout << "main thread message = " << s << std::endl;
  t.join();
  return 0;
}

從下面的輸出中,我們知道字符串已經(jīng)成功地傳遞給了線程函數(shù)。

thread function message is = Kathy Perry
main thread message = Kathy Perry

如果我們想以引用方式傳遞,我們可能會這樣做:

void thread_function(std::string &s)
{
  std::cout << "thread function ";
  std::cout << "message is = " << s << std::endl;
  s = "Justin Beaver";
}

為確保字符串真的是以引用方式傳遞,我們在線程函數(shù)尾部修改了字符串的內(nèi)容。但是輸出并沒有改變:

thread function message is = Kathy Perry
main thread message = Kathy Perry

實(shí)際上,字符串是以值傳遞的而不是引用傳遞。為了以引用方式傳遞,我們應(yīng)該使用std::ref稍微修改下代碼:

std::thread t(&thread;_function, std::ref(s));

然后,修改后的輸出為:

thread function message is = Kathy Perry
main thread message = Justin Beaver

有另一種免復(fù)制、非內(nèi)存共享的方法在線程間傳遞參數(shù)。 我們可以使用 move():

std::thread t(&thread_function, std::move(s));

當(dāng)字符串被從main函數(shù)移動到線程函數(shù)后,main函數(shù)就不再有這個(gè)變量了,輸出為空值:

thread function message is = Kathy Perry
main thread message =

線程復(fù)制和移動 copy / move

如下代碼期望拷貝線程是編譯不通過的:

#include <iostream>
#include <thread>

void thread_function()
{
  std::cout << "thread function\n";
}

int main()
{
  std::thread t(&thread;_function);
  std::cout << "main thread\n";
  std::thread t2 = t;

  t2.join();

  return 0;
}

但是我們可以通過move把其所有權(quán)轉(zhuǎn)移,見如下代碼:

// t5.cpp
#include <iostream>
#include <thread>

void thread_function()
{
  std::cout << "thread function\n";
}

int main()
{
  std::thread t(&thread;_function);
  std::cout << "main thread\n";
  std::thread t2 = move(t);

  t2.join();

  return 0;
}

程序輸出:

$ g++ t5.cpp -o t5 -std=c++11 -pthread
$ ./t5
main thread
thread function 

線程ID

我們可以通過 this_thread::get_id()獲取線程的id信息:

int main()
{
  std::string s = "Kathy Perry";
  std::thread t(&thread_function, std::move(s));
  std::cout << "main thread message = " << s << std::endl;

  std::cout << "main thread id = " << std::this_thread::get_id() << std::endl;
  std::cout << "child thread id = " << t.get_id() << std::endl;

  t.join();
  return 0;
}

輸出:

thread function message is = Kathy Perry
main thread message =
main thread id = 1208
child thread id = 5224

創(chuàng)建多少線程呢?

線程庫提供了線程數(shù)量的建議函數(shù)hardware_concurrency():

int main()
{
  std::cout << "Number of threads = " 
       << std::thread::hardware_concurrency() << std::endl;
  return 0;
}

輸出:

Number of threads = 2

Lambda函數(shù)

既然我們談的C++,那么讓我們來了解下Lambda。

我們可以用lambda函數(shù)(匿名函數(shù))這樣替換線程函數(shù):

int main()
{
  std::thread t([]()
  {
    std::cout << "thread function\n";
  }
  );
  std::cout << "main thread\n";
  t.join();   // main thread waits for t to finish
  return 0;
}

注意,我們正在編寫內(nèi)聯(lián)代碼,并將其傳遞到另一個(gè)線程構(gòu)造函數(shù)中。

Lambda表達(dá)式是用括號括起來的一系列語句, 前綴用[], 調(diào)用lambda編譯接口告訴編譯器我們正在聲明一個(gè)lambda函數(shù), 在我們的例子中,沒有傳遞參數(shù)。我們本質(zhì)上可以用 {} 作為一個(gè)任務(wù) , 并把它分配給我們的線程。

關(guān)于“C++11/14中線程如何調(diào)用類對象和線程傳參”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

新聞標(biāo)題:C++11/14中線程如何調(diào)用類對象和線程傳參
分享鏈接:http://muchs.cn/article20/pphjco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、、標(biāo)簽優(yōu)化、App開發(fā)網(wǎng)站制作、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作