本文介绍了std :: thread和rvalue参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开设某种委托人课程。下面是我方法的简化版本,它的主要功能是启动新线程执行某些操作(在本示例中,它每秒打印文本):

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)
    );
}

我担心的是,std :: thread构造函数具有以下签名:

My concern is, that std::thread constructor has following signature:

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

因此,它将右值引用作为第一个和第二个参数。如果是这样的话,那么在将它传递给 std :: thread 构造函数之后,我不应该使用 shouldContinue strong>已移动。

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来停止被调用的函数。出于明显的原因,我不想将此变量设置为全局变量。

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. For obvious reasons I do not want to make this variable global.

我认为, std :: ref 可以使那里有一些魔术,但是我仍然不确定它是如何工作的(在创建新线程时,在某些示例中我看到了 std :: ref )。

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).

我试图一点都不在乎这个事实,这是右值引用,后来我使用了 shouldContinue ,但是没有崩溃,但是我担心只是未定义的行为。谁能说出上面的代码是否正确,如果不正确,该如何正确执行?

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. 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:

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

何时&&&出现在类型推导的上下文中,T& amp;&&具有特殊的含义。当实例化func时,T取决于传递给func的参数是左值还是右值。如果它是类型U的左值,如果是右值,则将T推导为U:

"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:"

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.

这篇关于std :: thread和rvalue参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:21