通过引用C++11中的std::thread来传递对象

Passing object by reference to std::thread in C++11

本文关键字:thread 对象 std 引用 C++11 中的      更新时间:2023-10-16

为什么在创建std::thread时不能通过引用传递对象?

例如,下面的snippit给出了一个编译错误:

#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...>)
         ^

我已经改为传球了,但还有更好的方法吗?

使用std::ref:用reference_wrapper显式初始化线程

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

(或者std::cref而不是std::ref,视情况而定)。根据std:thread:上cppreference的注释

线程函数的参数是按值移动或复制的。如果引用参数需要传递给线程函数,则必须对其进行包装(例如使用std::refstd::cref)。

基于此注释,此答案详细说明了参数未通过引用传递给线程函数的原因。

考虑以下函数SimpleThread():

void SimpleThread(int& i) {
    std::this_thread::sleep_for(std::chrono::seconds{1});
    i = 0;
}

现在,想象一下如果编译了以下代码(它编译的是而不是),会发生什么

int main()
{
    {
        int a;
        std::thread th(SimpleThread, a);
        th.detach();
    }
    // "a" is out of scope
    // at this point the thread may be still running
    // ...
}

参数a将通过引用SimpleThread()来传递。在变量a已经超出作用域并且其生存期已经结束之后,线程可能仍然在函数SimpleThread()中休眠。如果是这样,SimpleThread()中的i实际上将是悬挂引用,并且分配i = 0将导致未定义行为

通过使用类模板std::reference_wrapper包装引用参数(使用函数模板std::refstd::cref),可以明确地表达您的意图。

std::thread复制(/move)其参数,您甚至可能会看到注释:

线程函数的参数是按值移动或复制的。如果引用参数需要传递给线程函数,则必须对其进行包装(例如,使用std::refstd::cref)。

因此,您可以使用std::reference_wrapperstd::ref/std::cref:

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

或使用lambda:

auto thread1 = std::thread([&a]() { SimpleThread(a); });

不要通过引用传递如果您的对象是分配的基于堆栈的,请通过指针传递到线程API调用中创建的新对象。这样的对象将和线程一样长,但应该在线程终止之前显式删除。

示例:

void main(){
...
std::string nodeName = "name_assigned_to_thread";
std::thread nodeThHandle = std::thread(threadNODE, new std::string(nodeName));
...
}
void threadNODE(std::string *nodeName){
/* use nodeName everywhere but don't forget to delete it before the end */
delete nodeName;
}