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.