在 C++11 中通过引用 std::thread 传递对象
Passing object by reference to std::thread in C++11
为什么在创建 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...>)
^
我已更改为传递指针,但有更好的解决方法吗?
显式初始化线程 reference_wrapper
by using std::ref
:
auto thread1 = std::thread(SimpleThread, std::ref(a));
(或 std::cref
而不是 std::ref
,视情况而定)。根据 cppreference on std:thread
的注释:
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
).
基于,这个答案详细阐述了参数不通过引用传递给线程函数的原因默认.
考虑以下函数 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
将导致 未定义的行为.
通过使用 class 模板 std::reference_wrapper
(使用函数模板 std::ref
和 std::cref
)包装引用参数,您可以明确表达您的意图。
如果您的对象是基于堆栈分配的,请不要通过引用传递,通过指向调用中创建的新对象的指针传递线程 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;
}
std::thread
复制(/移动)其参数,您甚至可能会看到注释:
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
).
因此,您可以使用 std::reference_wrapper
through std::ref
/std::cref
:
auto thread1 = std::thread(SimpleThread, std::ref(a));
或使用 lambda:
auto thread1 = std::thread([&a]() { SimpleThread(a); });
为什么在创建 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...>)
^
我已更改为传递指针,但有更好的解决方法吗?
显式初始化线程 reference_wrapper
by using std::ref
:
auto thread1 = std::thread(SimpleThread, std::ref(a));
(或 std::cref
而不是 std::ref
,视情况而定)。根据 cppreference on std:thread
的注释:
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
orstd::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
将导致 未定义的行为.
通过使用 class 模板 std::reference_wrapper
(使用函数模板 std::ref
和 std::cref
)包装引用参数,您可以明确表达您的意图。
如果您的对象是基于堆栈分配的,请不要通过引用传递,通过指向调用中创建的新对象的指针传递线程 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;
}
std::thread
复制(/移动)其参数,您甚至可能会看到注释:
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
orstd::cref
).
因此,您可以使用 std::reference_wrapper
through std::ref
/std::cref
:
auto thread1 = std::thread(SimpleThread, std::ref(a));
或使用 lambda:
auto thread1 = std::thread([&a]() { SimpleThread(a); });