Std :: stream constructor passed by reference when using class member function

I have done quite a bit of research on the new value of C ++ 11 and have ported lvalue. Here is an example of what I found and read:

what-does-t-double-ampersand-mean-in-c11

how-stdthread-constructor-detects-rvalue-reference

stdthread-and-rvalue-reference

I also briefly talked about rvalue links

Move_Semantics

rvalue_references

Suggestion to add Rvalue C ++ link

In particular, regarding the constructor of std::thread I found

how-to-create-a-thread-inside-a-class-function

and used one of the answers to write 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 

Checking the constructor `std :: thread

std :: thread :: Thread

I am observing a pattern that uses rvalue parameters. I understand that std::thread can be started with a simple example

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

which appears straightforwardly until I get to the class in which I need to do the following

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

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

This makes sense, but the need to pass a class function by reference is not the way I saw A a; A&& ref = A() A a; A&& ref = A() . I am very confused about rvalues ​​and just want to understand why the code I wrote above needs to be passed to the std::thread function.

I am sure that I also do not understand the variational patterns that the std::thread varicic pattern constructor understands well.

+1
source share
1 answer

I am observing a pattern that uses rvalue parameters.

No, the std::thread constructor uses forwarding links, which means that they can accept arguments of any type and invoke lvalue or rvalue links depending on the type of argument.

Read the forwarding links and Universal Links in C ++ 11 (Scott Meyers introduced the name “universal link” for them, but the official name is now “forwarding link”).

It makes sense, but you need to pass a class function by reference, not

You are confused, &Consumer::run not a link of any kind. The & symbol as part of typename means a link, but when it is located to the left of the expression, it is the "address" operator that forms the pointer. In this case, it forms a pointer for a member function .

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

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

This is equivalent to running this->run()

+2
source

Source: https://habr.com/ru/post/1443767/


All Articles