简体   繁体   English

std :: thread和rvalue参考

[英]std::thread and rvalue reference

I wanted to have some kind of delegator class. 我想参加某种代表班。 Shortened version of my approach is below and it's main functionality is to start new thread doing some thing (in this example it prints text every second): 下面是我方法的简化版本,它的主要功能是启动新线程以执行某些操作(在此示例中,它每秒输出文本):

void Flusher::start(){
    m_continue.store(true);

    m_thread = std::thread([](std::atomic<bool>& shouldContinue){
        while(shouldContinue.load()){
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::cout << "sec passed" << std::endl;
        }}, std::ref<std::atomic<bool>>(m_continue)
    );
}

My concern is, that std::thread constructor has following signature: 我担心的是,std :: thread构造函数具有以下签名:

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );

So it takes rvalue reference as the first and second argument. 因此,它将右值引用作为第一个和第二个参数。 If it's so, then I should not use shouldContinue after passing it to the std::thread constructor as it was moved . 如果是这样,那我不应该使用shouldContinue它传递给后std::thread构造,因为它是感动

Of course I want to have control over this function and therefore I want to use shouldContinue in a caller thread to stop called function. 当然,我想对此函数进行控制,因此我想在调用者线程中使用shouldContinue来停止被调用的函数。 For obvious reasons I do not want to make this variable global. 由于明显的原因,我不想将此变量设置为全局变量。

I think, that std::ref makes some magic there, but I am still not sure how does it work (I saw std::ref in some examples when creating new thread). 我认为, std::ref在这里起到了神奇的作用,但是我仍然不确定它是如何工作的(在创建新线程时,在某些示例中我看到了std::ref )。

I tried to not care at all about the fact, this is rvalue reference and I used shouldContinue later on and nothing crashed, but I fear this is simply undefined behavior. 我试图一点都不在乎这个事实,这是右值引用,后来我使用了shouldContinue ,没有崩溃,但是我担心这只是未定义的行为。 Could anyone tell if above code is correct and if not, how to do this correctly? 谁能说出上面的代码是否正确,如果不正确,该如何正确执行?

There is a special type deduction rule when && is used with templates. 当&&与模板一起使用时,有一个特殊的类型推导规则。

Check this out for a really good explanation: 检查一下,以获得一个很好的解释:

http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/ http://eli.thegreenplace.net/2014/perfect-forwarding-and-universal-references-in-c/

template <class T>
void func(T&& t) {
}

"When && appears in a type-deducing context, T&& acquires a special meaning. When func is instantiated, T depends on whether the argument passed to func is an lvalue or an rvalue. If it's an lvalue of type U, T is deduced to U&. If it's an rvalue, T is deduced to U:" “当&&出现在类型推断上下文中时,T &&具有特殊含义。当实例化func时,T取决于传递给func的参数是左值还是右值。如果它是类型U的左值,则推导为U&。如果是右值,则将T推导为U:”

func(4);            // 4 is an rvalue: T deduced to int

double d = 3.14;
func(d);            // d is an lvalue; T deduced to double&

float f() {...}
func(f());          // f() is an rvalue; T deduced to float

int bar(int i) {
  func(i);          // i is an lvalue; T deduced to int&
}

Also, reference collapsing rule is a good read. 此外,参考折叠规则也是一本好书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM