简体   繁体   English

std :: thread参数(值与const)

[英]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.

相关问题 交换与使用std :: map []运算符分配(const问题) - Swapping vs. assigning with std::map [] operator (const issue) std::function - 值与参考参数 - std::function - value vs. reference argument C ++中的std :: thread与Go语言中的goroutine? - std::thread in C++ vs. goroutine in go language? 多进程 MPI 与多线程 std::thread 性能 - multi-process MPI vs. multithreaded std::thread performance 默认值作为全局常量与函数返回值 - Default value as Global const vs. Function return value C++11 线程等待行为:std::this_thread::yield() 与 std::this_thread::sleep_for( std::chrono::milliseconds(1) ) - C++11 Thread waiting behaviour: std::this_thread::yield() vs. std::this_thread::sleep_for( std::chrono::milliseconds(1) ) 参数传递礼仪(C ++)const&vs. value - Parameter Passing Etiquette (C++) const& vs. value 基于值 vs. 常量引用的函数重载 - Function Overloading Based on Value vs. Const Reference const引用临时值与返回值优化 - const reference to temporary vs. return value optimization std :: remove_if中的const参数 - const arguments in std::remove_if
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM