std::thread 和右值引用
std::thread and rvalue reference
我想要某种委托人class。我的方法的简化版本如下,它的主要功能是启动新线程做一些事情(在这个例子中它每秒打印一次文本):
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 构造函数具有以下签名:
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
所以它以右值引用作为第一个和第二个参数。如果是这样,那么我不应该在将 shouldContinue
传递给 std::thread
构造函数后使用 moved.
当然我想控制这个函数,因此我想在调用者线程中使用 shouldContinue 来停止被调用的函数。出于显而易见的原因,我不想将此变量设为全局变量。
我认为 std::ref
在那里发挥了一些魔力,但我仍然不确定它是如何工作的(我在创建新线程时在某些示例中看到了 std::ref
)。
我试图完全不关心这个事实,这是右值引用,后来我使用了 shouldContinue
并且没有崩溃,但我担心这只是未定义的行为。谁能告诉我上面的代码是否正确,如果不正确,如何正确执行此操作?
当 && 与模板一起使用时,有一个特殊的类型推导规则。
查看这个以获得非常好的解释:
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:"
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&
}
此外,参考折叠规则是一本好书。
我想要某种委托人class。我的方法的简化版本如下,它的主要功能是启动新线程做一些事情(在这个例子中它每秒打印一次文本):
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 构造函数具有以下签名:
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
所以它以右值引用作为第一个和第二个参数。如果是这样,那么我不应该在将 shouldContinue
传递给 std::thread
构造函数后使用 moved.
当然我想控制这个函数,因此我想在调用者线程中使用 shouldContinue 来停止被调用的函数。出于显而易见的原因,我不想将此变量设为全局变量。
我认为 std::ref
在那里发挥了一些魔力,但我仍然不确定它是如何工作的(我在创建新线程时在某些示例中看到了 std::ref
)。
我试图完全不关心这个事实,这是右值引用,后来我使用了 shouldContinue
并且没有崩溃,但我担心这只是未定义的行为。谁能告诉我上面的代码是否正确,如果不正确,如何正确执行此操作?
当 && 与模板一起使用时,有一个特殊的类型推导规则。
查看这个以获得非常好的解释:
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:"
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&
}
此外,参考折叠规则是一本好书。