[英]std::thread arguments (value vs. const)
When I generate a new thread ( std::thread
) with a function the arguments of that function are by value - not by reference. 当我使用函数生成新线程(
std::thread
)时,该函数的参数是按值而不是按引用的。
So if I define that function with a reference argument ( int& nArg
) my compiler (mingw 4.9.2) outputs an error (in compilian-suaeli something like "missing copy constructor" I guess ;-) 因此,如果我使用参考参数(
int& nArg
)定义该函数,则我的编译器(mingw 4.9.2)将输出错误(在compilian-suaeli中,类似“缺少复制构造函数”之类的;-)
But if I make that reference argument const ( const int& nArg
) it does not complain. 但是,如果我将该引用参数
const int& nArg
const( const int& nArg
),则不会抱怨。
Can somebody explain please? 有人可以解释一下吗?
If you want to pass reference, you have to wrap it into std::reference_wrapper
thanks to std::ref
. 如果要传递引用,则必须
std::reference_wrapper
std::ref
将其包装到std::reference_wrapper
。 Like: 喜欢:
#include <functional>
#include <thread>
void my_function(int&);
int main()
{
int my_var = 0;
std::thread t(&my_function, std::ref(my_var));
// ...
t.join();
}
std::thread
's arguments are used once. std::thread
的参数仅使用一次。
In effect, it stores them in a std::tuple<Ts...> tup
. 实际上,它将它们存储在
std::tuple<Ts...> tup
。 Then it does a f( std::get<Is>(std::move(tup))...)
. 然后执行
f( std::get<Is>(std::move(tup))...)
。
Passing std::get
an rvalue tuple
means that it is free to take the state from a value or rvalue reference field in the tuple. 传递
std::get
右值tuple
意味着可以从元组中的值或右值引用字段中获取状态。 Without the tuple being an rvalue, it instead gives a reference to it. 如果没有元组是右值,它会给它一个引用。
Unless you use reference_wrapper
(ie, std::ref
/ std::cref
), the values you pass to std::thread
are stored as values in the std::tuple
. 除非您使用
reference_wrapper
(即std::ref
/ std::cref
),否则您传递给std::thread
的值将作为值存储在std::tuple
。 Which means the function you call is passed an rvalue to the value in the std::tuple
. 这意味着您调用的函数被传递一个rvalue到
std::tuple
的值。
rvalues can bind to const&
but not to &
. 右值可以绑定到
const&
而不能绑定到&
。
Now, the std::tuple
above is an implementation detail, an imagined implementation of std::thread
. 现在,上面的
std::tuple
是一个实现细节,是std::thread
一个设想实现。 The wording in the standard is more obtuse. 标准中的措词较为晦涩。
Why does the standard say this happens? 为什么标准说发生这种情况? In general, you should not bind a
&
parameter to a value which will be immediately discarded. 通常,不应将
&
参数绑定到将立即丢弃的值。 The function thinks that it is modifying something that the caller can see; 该函数认为它正在修改调用者可以看到的内容。 if the value will be immediately discarded, this is usually an error on the part of the caller.
如果该值将被立即丢弃,则通常是调用者出错。
const&
parameters, on the other hand, do bind to values that will be immediately discarded, because we use them for efficiency purposes not just for reference purposes. 另一方面,
const&
参数会绑定到将立即丢弃的值,因为我们将它们用于效率目的而不仅仅是参考目的。
Or, roughly, because 或者,大概是因为
const int& x = 7;
is legal 是合法的
int& x = 7;
is not. 不是。 The first is a
const&
to a logically discarded object (it isn't due to reference lifetime extension, but it is logically a temporary). 第一个是对逻辑上被丢弃的对象的
const&
(不是由于引用生存期的延长,而是逻辑上是临时的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.