通过引用C++11中的std::thread来传递对象
Passing object by reference to std::thread in C++11
为什么在创建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::ref
或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
将导致未定义行为。
通过使用类模板std::reference_wrapper
包装引用参数(使用函数模板std::ref
和std::cref
),可以明确地表达您的意图。
std::thread
复制(/move)其参数,您甚至可能会看到注释:
线程函数的参数是按值移动或复制的。如果引用参数需要传递给线程函数,则必须对其进行包装(例如,使用
std::ref
或std::cref
)。
因此,您可以使用std::reference_wrapper
到std::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;
}
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- 在没有复制构造函数的对象的成员函数中启动 std::thread
- 如何在 UWP C++/CX 中对象的成员函数中使用 std::thread
- 线程在销毁包含该线程的对象时调用 thread.detach()
- 移动包含正在运行的 std::thread 成员的对象
- 将回调函数中对对象的引用传递给 std::thread
- 扩展 std::thread 的 ref 对象的传递范围
- 从具有本地 std::thread 对象而不连接的函数退出
- 构造std::thread对象过程中的详细信息
- 使用C++0x std::thread调用新ed对象中的成员函数
- 访问可调用对象 一个 boost::thread 是用来构造的
- 从另一个 std::thread 调用 Qt 对象方法
- 通过引用C++11中的std::thread来传递对象
- 为什么我不能使用 std::thread 通过引用发送对象
- Std::thread在对象中派生的线程,它何时终止?
- 可以从同一个线程中移动assign std::thread对象吗?
- 在超时后销毁thread和deadline_timer对象
- 使用 boost::thread 将 c++ 对象从一个线程传递到另一个线程的正确方法是什么?
- 是否可以替换std::thread对象