久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 C++11 中通過引用 std::thread 傳遞對象

Passing object by reference to std::thread in C++11(在 C++11 中通過引用 std::thread 傳遞對象)
本文介紹了在 C++11 中通過引用 std::thread 傳遞對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

為什么在創建 std::thread 時不能通過引用傳遞對象?

Why can't you pass an object by reference when creating a std::thread ?

例如下面的代碼段給出了一個編譯錯誤:

For example the following snippit gives a compile error:

#include <iostream>
#include <thread>

using namespace std;

static void SimpleThread(int& a)  // compile error
//static void SimpleThread(int a)     // OK
{
    cout << __PRETTY_FUNCTION__ << ":" << a << endl;
}

int main()
{
    int a = 6;

    auto thread1 = std::thread(SimpleThread, a);

    thread1.join();
    return 0;
}

錯誤:

In file included from /usr/include/c++/4.8/thread:39:0,
                 from ./std_thread_refs.cpp:5:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(int))(int&)>’:
/usr/include/c++/4.8/thread:137:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(int&); _Args = {int&}]’
./std_thread_refs.cpp:19:47:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(int))(int&)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<void (*(int))(int&)>’
         _M_invoke(_Index_tuple<_Indices...>)
         ^

我已改為傳遞指針,但有更好的解決方法嗎?

I've changed to passing a pointer, but is there a better work around?

推薦答案

使用 reference_wrapper 使用 std::ref:

Explicitly initialize the thread with a reference_wrapper by using std::ref:

auto thread1 = std::thread(SimpleThread, std::ref(a));

(或 std::cref 而不是 std::ref,視情況而定).根據 cppreference on std:thread 的注釋:

(or std::cref instead of std::ref, as appropriate). Per notes from cppreference on std:thread:

線程函數的參數按值移動或復制.如果需要將引用參數傳遞給線程函數,則必須對其進行包裝(例如使用 std::refstd::cref).

The arguments to the thread function are moved or copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g. with std::ref or std::cref).

這篇關于在 C++11 中通過引用 std::thread 傳遞對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 久久国产综合 | 手机看片欧美 | 国产精品久久久久久久久久久久午夜片 | 日韩欧美在线播放 | 人人超碰在线 | 欧美一区二区 | jlzzjlzz欧美大全| 久久夜色精品国产欧美乱极品 | 国产欧美一区二区三区视频在线观看 | 国内精品久久久久 | 亚洲国产黄色 | 亚洲欧美在线视频 | 久久久久成人网 | 中文字幕欧美激情 | a级黄色片 | 日韩精品福利 | 国产精品美女久久久 | 国产欧美日韩在线视频 | 亚洲精品观看 | 黄色一级片免费 | 国产永久视频 | 欧美激情在线观看 | 精产国产伦理一二三区 | 欧美日韩国产在线播放 | 国产午夜精品久久久久久久 | 中文在线观看视频 | 性猛交xxxx富婆老太婆 | 欧美一区二区在线视频 | 中文字幕在线观看不卡 | 青青草精品 | 午夜久久久久久久 | 免费网站黄色 | 色av吧| 中文一区二区 | 久久视频在线免费观看 | 亚洲一区免费视频 | 日韩精品一区二区在线 | 成人深夜福利视频 | 婷婷综合五月天 | 黄色一级视频免费看 | 日韩一区二区三区在线 |