c++11多线程---线程入口函数

时间:2021-01-29 16:20:47

1、普通函数(线程入口)

#include <thread>
#include <iostream> void hello(const char *name) {
std::cout << "Hello " << name << std::endl;
} int main() {
std::thread thread(hello, "C++11"); //函数名、参数
thread.join(); //等待线程执行完毕 return ;
}

2、类成员函数(线程入口)

#include <thread>
#include <iostream> class Greet
{
const char *owner = "Greet";
public:
void SayHello(const char *name) {
std::cout << "Hello " << name << " from " << this->owner << std::endl;
}
};
int main() {
Greet greet; std::thread thread(&Greet::SayHello, &greet, "C++11"); //传入成员函数地址、 类对象地址、参数
thread.join(); return ;
}
//输出:Hello C++11 from Greet

https://www.jianshu.com/u/88ad4f76eb79