#include<iostream>
#include<thread>
#include<memory>
using namespace std;
thread t2;
int b = 2;
void fun(int& b) {
b++;
}
void test1(int &a) {
a++;
}
void test2() {
t2 = thread(fun, ref(b));
}
void test3(int *c){
cout << "c" << *c << endl;
}
class Myclass {
private:
friend void thread_test();
void test4() {
cout << "d114514" << endl;
}
void test5() {
cout << "e1919810" << endl;
}
};
void thread_test() {
shared_ptr<Myclass> e = make_shared<Myclass>();
thread t5(&Myclass::test5, e);
t5.join();
}
int main() {
//1、传递临时变量
int a = 1;
thread t1(test1, ref(a));
t1.join();
cout <<"a"<< a << endl;
//2、传递指针或引用指向局部变量
test2();
t2.join();
cout << "b" << b << endl;
//3、传递指针或引用指向已释放的内存
int* c = new int(3);
thread t3(test3, c);
t3.join();
//4、类成员函数作为入口函数,类对象被提前释放(解法函数thread_test()内智能指针)
//5、入口函数为类的私有函数
thread_test();
return 0;
}