C++多线程 继续讨论多线程传参问题

王半仙儿不喜欢说废话,所以直接上代码

讨论一

class TestClassA
{
public:
	int myValue;
	//构造函数
	TestClassA(int v) :myValue(v) { cout << "this is 构造函数 " << "this is : " << this << "thread :" << this_thread::get_id(); }
	//拷贝函数
	TestClassA(TestClassA &a):myValue(a.myValue){ cout << "this is 拷贝函数 " << "this is : " << this << "thread :" << this_thread::get_id(); }
	//析构函数
	~TestClassA(){ cout << "this is 析构函数 " << "this is : " << this << "thread :" << this_thread::get_id(); }
};
//			这里如果不使用const,程序内部做的工作会更多,比如多复制一份对象
void print(const TestClassA& a)
{
	cout << "子线程的id是:" << this_thread::get_id() << endl;
}

int main()
{
	cout << "主线程id:" << this_thread::get_id() << endl;
	int value = 1;
	//						这里使用隐式转换的方式进行传递参数
	thread myThread(print, value);
	myThread.join();
    cout << "Hello World!\n";
}

执行的结果如下
在这里插入图片描述
可以看出,主线程的id是72888,而构造函数执行的线程是78576,这说明我们使用隐式转换传递参数时,隐式转换构造对象是在子线程内部完成的,这里使用了join还好,如果使用了detach方法,在子线程引用主线程的int类型的变量值时,主线程就已经运行结束销毁的话,那结果就完全不可预知了,这说明了一个问题

那么我们换一种思考方式来


int main()
{
	cout << "主线程id:" << this_thread::get_id() << endl;
	int value = 10;
	//构造一个临时变量传进去
	TestClassA a(value);
	thread myThread(print, a);
	myThread.join();
    cout << "Hello World!\n";
}

结果如下:
在这里插入图片描述
我们看到,主线程id是97388,对象的构造和拷贝,都是在主线程中进行的,这就说明了一个问题,我们将这个对象的引用传递给线程的执行函数,将不会造成不可预知的结果
结论

  • 在detach的情况下,多线程传递参数时,要尽量避免隐式转换的方式

讨论二 如何把对象本身传进去?

通过之前的试验,我们得出这样一条结论:
即使是函数声明时,接收的是引用,如果传递的不是基本类型,还是会拷贝一个新的对象进去
那我就想把这个对象传递进去,然后在里面修改对象的属性值,并且还能再外面生效,我们看看普通的做法:

方式一:直接传递临时变量
#include <iostream>
#include <thread>
using namespace std;

class TestClassA
{
public:
	//mutable也是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使函数或者变量被const声明
	mutable int myValue;
	//构造函数
	TestClassA(int v) :myValue(v) { cout << "this is 构造函数 " << "this is : " << this << "thread :" << this_thread::get_id() << endl; }
	//拷贝函数
	TestClassA(TestClassA &a):myValue(a.myValue){ cout << "this is 拷贝函数 " << "this is : " << this << "thread :" << this_thread::get_id()<< endl; }
	//析构函数
	~TestClassA(){ cout << "this is 析构函数 " << "this is : " << this << "thread :" << this_thread::get_id()<<endl; }
};

void print(const TestClassA &abcd)
{
	cout << "子线程的id是:" << this_thread::get_id() << endl;
	abcd.myValue = 9999;
	cout << "子线程中属性值" << abcd.myValue << endl;
}

int main()
{
	cout << "主线程id:" << this_thread::get_id() << endl;
	int value = 10;
	TestClassA a(value);
	thread myThread(print, a);
	myThread.join();
	cout << "主线程中属性值" << a.myValue << endl;
    cout << "Hello World!\n";
}

结果如下
在这里插入图片描述
可以看出,并没有改变主线程中的变量值,从之前的试验我们也可以看到,是因为传进去的是根据该对象拷贝的对象,内存地址都不一样

结论:
直接传递临时变量不能把对象本身传递进去

方式二:std::ref

改变main函数


int main()
{
	cout << "主线程id:" << this_thread::get_id() << endl;
	int value = 10;
	TestClassA a(value);
	thread myThread(print, std::ref(a));
	myThread.join();
	cout << "主线程中属性值" << a.myValue << endl;
    cout << "Hello World!\n";
}

在这里插入图片描述
可以看出,这一次,不但主线程和子线程中对象的属性值一样,并且拷贝函数也没有执行,这说明:
如果接受参数是使用的是引用,使用std::red传递进去的参数就是这个参数本身,不管这个参数是不是基本类型

方式三:指针

在这里插入图片描述

在这里插入图片描述

可以看出,指针指的确实是同一块儿内存,所以改变属性值,外面一定改变
如果可以优化的话,那就是用智能指针吧,可以自动释放内存,非常优秀


void print2(unique_ptr<int> ptr2)
{
	cout << ptr2 << endl;
}

int main()
{
	unique_ptr<int> ptr(new int(13));
	print2(std::move(ptr));
	cout << "hello world" << endl;
}

讨论二 实用类的成员函数作为线程的起始函数

class TestClassA
{
public:
	//mutable用法:
	mutable int myValue;
	//构造函数
	TestClassA(int v) :myValue(v) { cout << "this is 构造函数 " << "this is : " << this << "thread :" << this_thread::get_id() << endl; }
	//拷贝函数
	TestClassA(TestClassA &a):myValue(a.myValue){ cout << "this is 拷贝函数 " << "this is : " << this << "thread :" << this_thread::get_id()<< endl; }
	//析构函数
	~TestClassA(){ cout << "this is 析构函数 " << "this is : " << this << "thread :" << this_thread::get_id()<<endl; }
	//任意一个函数
	void anyStartMethod(int num)
	{
		cout << "子线程的起始函数执行" << "this is : " << this << "thread :" << this_thread::get_id() << endl;
	}
};

int main()
{
	TestClassA tca(10);
	//第一个参数,说明是什么类的什么方法,第二个参数,哪个对象,第三参数,参数
	thread myThread(&TestClassA::anyStartMethod, tca, 15);
	myThread.join();
}

执行结果:
在这里插入图片描述
看结果,执行了拷贝函数。
我们稍微改变一下main方法:

int main()
{
	TestClassA tca(10);
	thread myThread(&TestClassA::anyStartMethod, std::ref(tca), 15);
	myThread.join();
}

这样再次执行,结果如下:
在这里插入图片描述
这次拷贝函数没有执行,是不是感觉和之前的效果很象,实际上不是很像,就是一样
可以得出结论
即使线程的起始函数是某个类的成员方法,把这个类的某个实例传递进去,照样也要执行拷贝函数,实际上用的也是拷贝出来的函数,所以起始方法,仅仅只是个方法罢了

如果我们使用默认的类的线程启动方法opreator呢?
请看下面两幅图片
在这里插入图片描述
在这里插入图片描述
这两幅图更是证明了这个结论
即使线程的起始函数是某个类的成员方法,把这个类的某个实例传递进去,照样也要执行拷贝函数,实际上用的也是拷贝出来的函数,所以起始方法,仅仅只是个方法罢了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值