在C++ 11中通过引用传递对象到std :: thread

aus*_*ton 42 c++ pass-by-reference c++11 stdthread

为什么在创建时不能通过引用传递对象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;
}
Run Code Online (Sandbox Code Playgroud)

错误:

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...>)
         ^
Run Code Online (Sandbox Code Playgroud)

我改变了传递指针,但有更好的解决方法吗?

Sha*_*ger 61

明确初始化一个线程reference_wrapper使用std::ref:

auto thread1 = std::thread(SimpleThread, std::ref(a));
Run Code Online (Sandbox Code Playgroud)

(或视情况std::crefstd::ref定).来自cppreference的std:thread注释:

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

  • 明智地,捕获默认是按值捕获,否则如果参数在线程读取之前消失,则事情会非常失败.您需要明确地询问此行为,以便表明您负责确保引用的目标仍然有效. (12认同)
  • 优秀!我希望有一种方法可以明确传递引用.C++ 11再次救援:) (3认同)

眠りネ*_*ネロク 12

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

考虑以下功能SimpleThread()

void SimpleThread(int& i) {
    std::this_thread::sleep_for(std::chrono::seconds{1});
    i = 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,想象一下如果编译以下代码(编译)会发生什么:

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
    // ...
}
Run Code Online (Sandbox Code Playgroud)

该参数a 将通过引用传递SimpleThread()SimpleThread()在变量a已经超出范围并且其生存期结束之后,线程可能仍在函数中处于休眠状态。如果是这样,iin SimpleThread()实际上将是一个悬空的引用,并且该赋值i = 0将导致未定义的行为

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

  • 我认为这是关于“为什么参数不通过引用传递”的某种错误论证,因为即使使用“std::ref(a)”也会在这种情况下导致未定义的行为,不是吗? (2认同)
  • 必须编写“std::ref(a)”(而不仅仅是“a”)来通过引用传递“a”,可以防止您意外地认为“a”是按值传递的。 (2认同)

Jar*_*d42 10

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

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

因此,您可以std::reference_wrapper通过std::ref/std::cref使用:

auto thread1 = std::thread(SimpleThread, std::ref(a));
Run Code Online (Sandbox Code Playgroud)

或使用 lambda:

auto thread1 = std::thread([&a]() { SimpleThread(a); });
Run Code Online (Sandbox Code Playgroud)