【问题标题】:Passing object by reference to std::thread in C++11在 C++11 中通过引用 std::thread 传递对象
【发布时间】:2021-11-27 21:41:26
【问题描述】:

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

例如,以下代码段给出了编译错误:

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

我已经改为传递指针,但有更好的解决方法吗?

【问题讨论】:

    标签: c++ multithreading c++11 pass-by-reference stdthread


    【解决方案1】:

    std::threadcopy(/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); });
    

    【讨论】:

      【解决方案2】:

      如果你的对象是基于栈的分配的,不要通过引用,通过指针传递到在线程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;
      }
      

      【讨论】:

        【解决方案3】:

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

        考虑以下函数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)包装引用参数,您可以明确表达您的意图。

        【讨论】:

        • 我认为这是why arguments are not passed by reference 的某种错误论点,因为即使在这种情况下使用std::ref(a) 也会导致未定义的行为,不是吗?
        • @ampawd 是的,使用std::ref(a) 仍然会导致未定义的行为。但是,显式输入 std::ref(a) 会警告您不要通过引用传递 a,而仅输入 a 则不会。
        • 必须写std::ref(a)(而不仅仅是a)来通过引用传递a可以防止你意外地认为a是通过值传递的。
        【解决方案4】:

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

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

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

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

        【讨论】:

        • 捕获,明智地,默认为按值捕获,否则如果参数在线程读取它之前就消失了,事情会严重失败。您需要明确要求这种行为,以便表明您正在承担确保引用目标仍然有效的责任。
        • 太棒了!我希望有一种方法可以明确地传递参考。 C++11 再次救援 :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-29
        • 2013-08-11
        相关资源
        最近更新 更多