最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++11/14 線程調(diào)用類對(duì)象和線程傳參的方法

 更新時(shí)間:2019年01月22日 15:08:31   作者:lesliefish  
這篇文章主要介紹了C++11/14 線程調(diào)用類對(duì)象和線程傳參的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

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

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

#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ù)對(duì)象,并將其分配給線程任務(wù),我們可能會(huì)用這種方法嘗試此實(shí)例:

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

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

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

就是說(shuō)我們必須添加 ()包含 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

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

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

為確保字符串真的是以引用方式傳遞,我們?cè)诰€程函數(shù)尾部修改了字符串的內(nèi)容。但是輸出并沒(mé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ù)移動(dòng)到線程函數(shù)后,main函數(shù)就不再有這個(gè)變量了,輸出為空值:

thread function message is = Kathy Perry
main thread message =

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

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

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

但是我們可以通過(guò)move把其所有權(quán)轉(zhuǎn)移,見(jià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

我們可以通過(guò) 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)建多少線程呢?

線程庫(kù)提供了線程數(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++,那么讓我們來(lái)了解下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á)式是用括號(hào)括起來(lái)的一系列語(yǔ)句, 前綴用[], 調(diào)用lambda編譯接口告訴編譯器我們正在聲明一個(gè)lambda函數(shù), 在我們的例子中,沒(méi)有傳遞參數(shù)。我們本質(zhì)上可以用 {} 作為一個(gè)任務(wù) , 并把它分配給我們的線程。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • ipv6實(shí)現(xiàn)tcp編程示例

    ipv6實(shí)現(xiàn)tcp編程示例

    這篇文章主要介紹了ipv6實(shí)現(xiàn)tcp編程示例,需要的朋友可以參考下
    2014-03-03
  • 詳解QListWidget如何實(shí)現(xiàn)自定義Item效果

    詳解QListWidget如何實(shí)現(xiàn)自定義Item效果

    這篇文章主要為大家介紹了如何通過(guò)QListWidget實(shí)現(xiàn)自定義Item效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-04-04
  • 基于C語(yǔ)言實(shí)現(xiàn)關(guān)機(jī)小游戲的示例代碼

    基于C語(yǔ)言實(shí)現(xiàn)關(guān)機(jī)小游戲的示例代碼

    關(guān)機(jī)會(huì)寫吧!猜數(shù)字會(huì)寫吧!本文將結(jié)合這兩個(gè)功能,用C語(yǔ)言編寫一個(gè)關(guān)機(jī)惡搞小游戲(最好的朋友轉(zhuǎn)瞬即逝),只要猜對(duì)了,1分鐘后執(zhí)行關(guān)機(jī),除非輸入“我是豬”,但是輸完后,1分鐘后還是會(huì)執(zhí)行關(guān)機(jī),該保存保存,感興趣的可以嘗試一下
    2022-07-07
  • 深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制

    深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制

    信號(hào)和槽機(jī)制是 Qt 的核心機(jī)制,可以讓編程人員將互不相關(guān)的對(duì)象綁定在一起,實(shí)現(xiàn)對(duì)象之間的通信,本文就來(lái)深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制,感興趣的可以了解一下
    2023-08-08
  • C++實(shí)現(xiàn)LeetCode(9.驗(yàn)證回文數(shù)字)

    C++實(shí)現(xiàn)LeetCode(9.驗(yàn)證回文數(shù)字)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(9.驗(yàn)證回文數(shù)字),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++實(shí)現(xiàn)LeetCode(14.最長(zhǎng)共同前綴)

    C++實(shí)現(xiàn)LeetCode(14.最長(zhǎng)共同前綴)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(14.最長(zhǎng)共同前綴),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • c語(yǔ)言獲取用戶輸入字符串是scanf和gets的區(qū)別詳解

    c語(yǔ)言獲取用戶輸入字符串是scanf和gets的區(qū)別詳解

    今天小編就為大家分享一篇c語(yǔ)言獲取用戶輸入字符串是scanf和gets的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • C/C++舉例講解關(guān)鍵字的用法

    C/C++舉例講解關(guān)鍵字的用法

    相對(duì)于其他語(yǔ)言來(lái)說(shuō),C語(yǔ)言的關(guān)鍵字算是少的了。在C98中關(guān)鍵子總共只有32個(gè),我們來(lái)分析一下部分關(guān)鍵字在C/C++中它獨(dú)特的作用
    2022-05-05
  • C語(yǔ)言每日練習(xí)之二叉堆

    C語(yǔ)言每日練習(xí)之二叉堆

    這篇文章主要為大家介紹了C語(yǔ)言二叉堆,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • 深入c語(yǔ)言continue和break的區(qū)別詳解

    深入c語(yǔ)言continue和break的區(qū)別詳解

    本篇文章是對(duì)c語(yǔ)言中continue和break的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論

岳西县| 昂仁县| 海南省| 灵川县| 拉萨市| 岳西县| 托里县| 海晏县| 建瓯市| 时尚| 当阳市| 东丽区| 栾川县| 乐山市| 长阳| 手游| 永修县| 浮梁县| 嘉兴市| 内乡县| 张家港市| 田东县| 剑河县| 攀枝花市| 青海省| 本溪市| 香港| 上饶县| 江永县| 望奎县| 马尔康县| 宝兴县| 锡林浩特市| 铜鼓县| 侯马市| 拉萨市| 泰宁县| 泸州市| 黎城县| 铜山县| 招远市|