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::cref
而std::ref
定).来自cppreference的std:thread
注释:
线程函数的参数按值移动或复制.如果需要将引用参数传递给线程函数,则必须将其包装(例如,使用
std::ref
或std::cref
).
眠りネ*_*ネロク 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
已经超出范围并且其生存期结束之后,线程可能仍在函数中处于休眠状态。如果是这样,i
in SimpleThread()
实际上将是一个悬空的引用,并且该赋值i = 0
将导致未定义的行为。
通过使用类模板包装参考参数std::reference_wrapper
(使用函数模板std::ref
和std::cref
),您可以明确表达自己的意图。
Jar*_*d42 10
std::thread
复制(/移动)其参数,您甚至可能会看到注释:
线程函数的参数按值移动或复制。如果需要将引用参数传递给线程函数,则必须将其包装起来(例如,使用
std::ref
orstd::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)
归档时间: |
|
查看次数: |
18611 次 |
最近记录: |