C++11/14中線程如何調用類對象和線程傳參

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

創新互聯建站專注于西盟網站建設服務及定制,我們擁有豐富的企業做網站經驗。 熱誠為您提供西盟營銷型網站建設,西盟網站制作、西盟網頁設計、西盟網站官網定制、微信小程序服務,打造西盟網絡公司原創品牌,更為您提供西盟網站排名全網營銷落地服務。

線程調用類對象

在前面的示例中,我們為線程任務使用了通常的函數。實際上,我們可以使用任何可調用對象或者lambda函數,如下調用類對象的例子:

#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;
}

 在這里,我們創建了一個函數對象,并將其分配給線程任務,我們可能會用這種方法嘗試此實例:

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

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

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

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

為什么?我這邊不去深究,它跟C++函數聲明規定相關。

線程傳參

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

#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;
}

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

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";
}

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

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

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

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

然后,修改后的輸出為:

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

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

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

當字符串被從main函數移動到線程函數后,main函數就不再有這個變量了,輸出為空值:

thread function message is = Kathy Perry
main thread message =

線程復制和移動 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把其所有權轉移,見如下代碼:

// 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

創建多少線程呢?

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

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

輸出:

Number of threads = 2

Lambda函數

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

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

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;
}

注意,我們正在編寫內聯代碼,并將其傳遞到另一個線程構造函數中。

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

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

新聞標題:C++11/14中線程如何調用類對象和線程傳參
分享鏈接:http://m.kartarina.com/article20/pphjco.html

成都網站建設公司_創新互聯,為您提供用戶體驗標簽優化App開發網站制作App設計

廣告

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

外貿網站制作
主站蜘蛛池模板: 无码国内精品久久人妻麻豆按摩| 亚洲一区二区无码偷拍| 国产精品无码翘臀在线观看| 久久青青草原亚洲av无码| 八戒理论片午影院无码爱恋| 在线看片福利无码网址| 亚洲?V无码乱码国产精品| 国产真人无码作爱视频免费| 亚洲看片无码在线视频| 亚洲精品无码永久在线观看你懂的| 久久午夜无码鲁丝片| 久久国产精品无码网站| 色综合99久久久无码国产精品| 久久精品无码精品免费专区| 国产强被迫伦姧在线观看无码 | 亚洲精品无码激情AV| 无码乱码av天堂一区二区| 中文无码制服丝袜人妻av| 国产精品无码免费视频二三区| 日韩精品中文字幕无码一区| 国产AV无码专区亚洲精品| 久久久无码精品亚洲日韩软件| 无码人妻aⅴ一区二区三区有奶水| 日韩精品久久无码人妻中文字幕| 曰批全过程免费视频在线观看无码| 亚洲成在人线在线播放无码| 色欲狠狠躁天天躁无码中文字幕| 永久免费av无码网站yy| 国产亚洲?V无码?V男人的天堂 | 亚洲色无码一区二区三区| 精品少妇人妻av无码专区| 久久精品无码一区二区三区免费 | 人妻av中文字幕无码专区| 久久av无码专区亚洲av桃花岛 | 无码国产精成人午夜视频不卡| 精品三级AV无码一区| 无码中文字幕日韩专区| 日韩AV无码久久一区二区| 日韩精品无码免费一区二区三区| 亚洲国产精品无码成人片久久| 亚洲动漫精品无码av天堂|