When we are using thread to create a thread, we want to separate the threads, run a function, we just use the main thread, Std::thread (function name, parameter) can be
(If you don't understand, see: "Simple use of c++11 multithreaded Std::thread")
However, sometimes we want to open a thread and run a function inside a class.
For example: We have a class love with a member function shit (int)
What if we want to open a single thread and run the shit function?
The simple code is as follows:
#include"stdafx.h"#include<chrono>//Std::chrono::seconds#include <iostream>//Std::cout#include <thread>//Std::thread, Std::this_thread::sleep_forclasslove{ Public: Love (); ~Love (); voidShit (intding);}; Love::~Love () {}voidLove::shit (intDing) {Std::cout<< Ding <<"hahaha"<<Std::endl;}/** = = FUNCTION =========================================================* name:main* description:progra M entry routine.* ========================================================================*/intMainintargcConst Char*argv[]) {Love ABC;Std::thread TT (&love::shit,5); Tt.join ();return;} /*----------End of function main----------*/
We found a full compilation, but Ah!! There's Wood there!
Let's take a look at the main program, we'll set a Love class object abc
Then use
Std::thread TT (&love::shit,5
Want to thread, call the Love class inside the shit function, pass the parameter 5.
But the compilation does not pass.
Because the function inside the class, there is no object, how can it be called? So compile error ...
Therefore, we use
Std::thread TT (&love::shit,abc,5
We pass the object in, so the compilation is passed ...
PS: Pass "Object" in ... Oh, my God! What a lunatic! The object went in, I have to find the object again ah ...
C++11 Multithreading Std::thread Calling a method of a function in a class