久久久久久久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 排序功能對列表進行排序)
主站蜘蛛池模板: 婷婷开心激情综合五月天 | 成人精品鲁一区一区二区 | 亚洲国产一区二区三区在线观看 | 国产精品亚洲第一区在线暖暖韩国 | 欧美99| 国产精品久久久久久久岛一牛影视 | 手机在线观看av | 日韩欧美久久精品 | 欧美成人影院在线 | 精品福利一区二区三区 | 欧美a级成人淫片免费看 | 神马久久久久久久久久 | 国产91视频免费 | 国产精品一区二区三区99 | 97人人澡人人爽91综合色 | 一区二区三区在线看 | 亚洲成a人片 | 国产免费一区二区三区最新6 | 91影院在线观看 | www.久久 | 91啪影院| 久久精品男人的天堂 | 色男人天堂av | 欧美成人精品一区二区男人看 | 天天看片天天干 | 午夜伊人 | 日韩中文字幕 | 成年人在线观看视频 | 国产精品欧美一区二区 | 日韩国产在线 | 成人中文字幕在线观看 | 狠狠综合久久av一区二区小说 | 日韩中文字幕在线观看 | 美女毛片| 国产91综合一区在线观看 | 国产视频线观看永久免费 | 欧美成人aaa级毛片在线视频 | 日韩欧美大片在线观看 | 日韩一区二区在线视频 | 国产精品夜夜春夜夜爽久久电影 | 亚洲一区二区三区免费在线观看 |