相关疑难解决方法(0)

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

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

c++ pass-by-reference c++11 stdthread

42
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

pass-by-reference ×1

stdthread ×1