使用类成员函数时std :: thread构造函数按引用传递 [英] std::thread constructor pass by reference when using a class member function

查看:403
本文介绍了使用类成员函数时std :: thread构造函数按引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新的c ++ 11 rvalue进行了很多研究,并沿用了lvalue。
以下是我发现并阅读的内容的示例:

I've done quite a bit of research on the new c++11 rvalue and carried over lvalue. Here is a sample of what I have found and read:


how-stdthread-constructor-detects-rvalue-reference

stdthread-and-rvalue-reference

我也向我简要介绍了右值引用

I have also briefed myself on rvalue references


运动语义

rvalue_references

添加对C ++语言的右值引用的建议

关于 std :: thread 构造函数,我发现


如何在类函数内部创建线程

并使用答案之一来编写一些简单的代码

and used one of the answers to write some simple code

#pragma once
#ifndef CONSUMER_H
#define CONSUMER_H

#include "Mailbox.h"
#include <thread>
#include <iostream>

class Consumer
{
private:
    Mailbox mailbox;
    std::thread consumer;
public:
    Consumer(Mailbox& newMailbox);
    ~Consumer();
    void operator()() { std::cout << consumer.get_id() << "starting\n"; }
    void start();
    void run();
};

Consumer::Consumer(Mailbox& newMailbox)
{
    this->mailbox = newMailbox;
}

void Consumer::start()
{
    consumer = std::thread(&Consumer::run, this); <-- need understanding
}

#endif

检查`std :: thread构造函数

Checking the `std::thread constructor


std :: thread :: Thread

我观察到一个使用右值参数的模板。我知道可以通过一个简单的示例来启动 std :: thread

I observe a template that uses rvalue parameters. I understand that a std::thread can be initiated through a simple example

void run(void) {std::cout << "I'm running";}

std::thread(run);

它会直截了当地出现,直到我进入需要进行以下操作的班级为止

which appears straight-forward until I am inside a class whereby I need to do the following

consumer = std::thread(&Consumer::run, this); <-- need understanding

因为我从乔纳森·韦克利(Jonathan Wakely)那里了解到run()是一个非静态成员函数,并且具有在对象上运行。如果您不告诉新线程,新线程应该如何知道在哪个对象上调用它?

because I learned from Jonathan Wakely that run() is a non-static member function and has to be run on an object. How is the new thread supposed to know which object to call it on if you don't tell it?

这是有道理的,但是不需要通过引用传递类函数因为我看过 A a; A&& ref = A()可能。我对rvalue感到非常困惑,只想了解为什么我上面编写的代码对于将函数传递给 std :: thread 是必要的。

This makes some sense but the need to pass by reference the class function doesn't since I have seen A a; A&& ref = A() possible. I'm super confused about rvalues and just want to understand why the code I wrote above is necessary to pass the function to std::thread.

我确定我也不太了解可变参数模板,这或者加深了我对 std :: thread 可变参数模板构造函数的理解。

I feel certain I also don't understand variadic templates that well either which compounds my understand of the std::thread variadic template constructor.

推荐答案


我观察到一个使用rvalue参数的模板。

I observe a template that uses rvalue parameters.

否, std :: thread 构造函数使用转发引用,这意味着它们可以接受任何类型的参数并推断左值或右值引用,具体取决于参数类型。

No, the std::thread constructor uses forwarding references, which means they can accept any type of argument and deduce either lvalue or rvalue references, depending on the argument type.

阅读转发引用 C ++ 11中的通用引用(Scott Meye rs为此引入了名称通用引用,但现在的正式名称是转发引用。)

Read forwarding references and Universal References in C++11 (Scott Meyers introduced the name "universal reference" for these, but the official name is now "forwarding reference").


不需要通过引用传递类函数

This makes some sense but the need to pass by reference the class function doesn't

您很困惑,& Consumer: :run 不是任何类型的引用。作为类型名一部分的符号& 表示引用,但是当它位于表达式左侧时,它是运算符的地址,它构成一个指针。在这种情况下,它会形成指向成员函数的指针

You're confused, &Consumer::run is not a reference of any kind. The symbol & as part of a typename means a reference, but when it apepars on the left of an expression it is the "address of" operator, which forms a pointer. In this case it is forming a pointer to member function.

表达式 std :: thread(& Consumer :: run,this)构造一个带有两个参数的线程,一个指向应该在新线程中运行的成员函数的指针,以及指向将要调用该成员函数的对象的指针。因此,在新线程中,将发生以下情况:

The expression std::thread(&Consumer::run, this) constructs a thread with two arguments, a pointer to the member function that should run in the new thread, and a pointer to the object that the member function will be called on. So in the new thread, something like this happens:

auto pointer_to_member_function = &Consumer::run;
auto pointer_to_object = this;
(pointer_to_object->.pointer_to_member_function)();

这等效于运行 this-> run()

这篇关于使用类成员函数时std :: thread构造函数按引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆