日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++11仔细地将参数传递给线程std::thread

發(fā)布時間:2023/12/15 c/c++ 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++11仔细地将参数传递给线程std::thread 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

要將參數(shù)傳遞給線程的可關(guān)聯(lián)對象或函數(shù),只需將參數(shù)傳遞給std::thread構(gòu)造函數(shù)。

默認(rèn)情況下,所有的參數(shù)都將復(fù)制到新線程的內(nèi)部存儲中。

看一個例子:

給線程傳遞單個參數(shù)

#include <iostream> #include <string> #include <thread>void threadCallback(int x, std::string str) {std::cout << "Passed Number = " << x << std::endl;std::cout << "Passed String = " << str << std::endl; } int main() {int x = 10;std::string str = "Sample String";std::thread threadObj(threadCallback, x, str);threadObj.join();return 0; }

怎樣給線程傳遞函數(shù)
不要將本地堆棧變量的地址傳遞給線程的回調(diào)函數(shù),因為線程1中的局部變量可能會超出范圍,但線程2仍然嘗試通過它的地址訪問它。
在這種情況下,訪問無效地址可能會導(dǎo)致不可預(yù)測的行為。
例如:

#include <iostream> #include <thread>void newThreadCallback(int* p) {std::cout << "Inside Thread : "" : p = " << *p << std::endl;std::chrono::milliseconds dura(1000);std::this_thread::sleep_for(dura);*p = 19; }void startNewThread() {int i = 10;std::cout << "Inside Main Thread : "" : i = " << i << std::endl;std::thread t(newThreadCallback, &i);t.detach();std::cout << "Inside Main Thread : "" : i = " << i << std::endl; }int main() {startNewThread();std::chrono::milliseconds dura(2000);std::this_thread::sleep_for(dura);return 0; }

同樣的,在將指針傳遞給位于heap上的線程時,要小心,因為某些線程可能在新線程嘗試訪問它之前刪除該內(nèi)存。
在這種情況下,訪問無效地址可能會導(dǎo)致不可預(yù)測的行為。

例如:

#include <iostream> #include <thread>void newThreadCallback(int* p) {std::cout << "Inside Thread : "" : p = " << *p << std::endl;std::chrono::milliseconds dura(1000);std::this_thread::sleep_for(dura);*p = 19; }void startNewThread() {int* p = new int();*p = 10;std::cout << "Inside Main Thread : "" : *p = " << *p << std::endl;std::thread t(newThreadCallback, p);t.detach();delete p;p = NULL; }int main() {startNewThread();std::chrono::milliseconds dura(2000);std::this_thread::sleep_for(dura);return 0; }

給線程傳遞引用

由于參數(shù)被復(fù)制到新的線程堆棧,所以,如果想通過常用的方式傳遞引用,如:

#include <iostream> #include <thread>void threadCallback(int const& x) {int& y = const_cast<int&>(x);y++;std::cout << "Inside Thread x = " << x << std::endl; }int main() {int x = 9;std::cout << "In Main Thread : Before Thread Start x = " << x << std::endl;std::thread threadObj(threadCallback, x);threadObj.join();std::cout << "In Main Thread : After Thread Joins x = " << x << std::endl;return 0; }

輸出為:

In Main Thread : Before Thread Start x = 9?
Inside Thread x = 10?
In Main Thread : After Thread Joins x = 9?

即使threadCallback接受參數(shù)作為引用,但是并沒有改變main中x的值,在線程引用外它是不可見的。
這是因為線程函數(shù)threadCallback中的x是引用復(fù)制在新線程的堆棧中的臨時值。

如何修改:
使用 std::ref
?

#include <iostream> #include <thread>void threadCallback(int const& x) {int& y = const_cast<int&>(x);y++;std::cout << "Inside Thread x = " << x << std::endl; }int main() {int x = 9;std::cout << "In Main Thread : Before Thread Start x = " << x << std::endl;std::thread threadObj(threadCallback, std::ref(x));threadObj.join();std::cout << "In Main Thread : After Thread Joins x = " << x << std::endl;return 0; }

輸出:

In Main Thread : Before Thread Start x = 9?

Inside Thread x = 10?

In Main Thread : After Thread Joins x = 10?

指定一個類的成員函數(shù)的指針作為線程函數(shù)。將指針傳遞給成員函數(shù)作為回調(diào)函數(shù),并將指針指向?qū)ο笞鳛榈诙€參數(shù)

本文轉(zhuǎn)自:c++11多線程編程(三):仔細(xì)地將參數(shù)傳遞給線程_小麒麟的成長之路-CSDN博客

#include <iostream> #include <thread>class DummyClass {public:DummyClass() { }DummyClass(const DummyClass& obj) { }void sampleMemberfunction(int x) {std::cout << "Inside sampleMemberfunction " << x << std::endl;} };int main() {DummyClass dummyObj;int x = 10;std::thread threadObj(&DummyClass::sampleMemberfunction, &dummyObj, x);threadObj.join();return 0; }

總結(jié)

以上是生活随笔為你收集整理的c++11仔细地将参数传递给线程std::thread的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。