#include<iostream>
#include<string>
#include<istream>
#include<thread>
using namespace std;
class MyClass
{
public:
MyClass() { cout << "构造函数" << endl; };
MyClass(const MyClass& m) { cout << "拷贝构造函数" << endl; };
~MyClass() { cout << "析构函数" << endl; };
void fun() {
cout << "某种行为" << endl;
}
private:
};
值拷贝
//void func_value(int a, MyClass mm) {
// cout << "值传递" << endl;
// return;
//}
指针传递
//void func_p(int* a, MyClass* mm) {
// cout << "指针传递" << endl;
// return;
//}
//引用传递
//void func_ref(int& a, MyClass& mm) {
// cout << "引用传递" << endl;
// return;
//}
//unique_ptr
//void func_uniqueptr(int& a, unique_ptr<MyClass> mm) {
// cout << "func_uniqueptr" << endl;
// return;
//}
void func_sharedptr(int& a, shared_ptr<MyClass> mm) {
cout << "func_sharedptr" << endl;
return;
}
int main() {
int aa = 10;
//MyClass my;
//unique_ptr<MyClass> my(new MyClass);
/*thread task1(func_value, aa, my);
task1.join();*/
//thread task2(func_p, &aa, &my);
task2.join();
//task2.detach();
/*thread task3(func_uniqueptr, ref(aa), move(my));
task3.detach();*/
shared_ptr<MyClass> my(new MyClass);
thread task4(func_sharedptr, ref(aa), ref(my));
task4.detach();
my->fun();
return 0;
}
c++ 多线程unique_ptr和shared_ptr
最新推荐文章于 2024-06-15 17:40:20 发布