使用成员函数作为线程人口函数现象观察

使用成员函数作为线程人口函数现象观察,第1张

使用成员函数作为线程人口函数现象观察
#include 
#include 
using namespace std;
class A {
public:
	A(int a) {
		std::cout << "构造函数执行了" << "this = " << this << " threadid = " << std::this_thread::get_id() << std::endl;
	}
	A(const A& a) {
		std::cout << "拷贝构造函数执行了" << "this = " << this << " threadid = " << std::this_thread::get_id() << std::endl;
	}
	~A() {
		std::cout << "析构函数执行了" << "this = " << this << " threadid = " << std::this_thread::get_id() << std::endl;
	}
public:
	void run(int ab) {
		std::cout << ab << std::endl;
		std::cout << "子线程ID = " << std::this_thread::get_id() <执行的。并且复制的动作是在主线程当中执行的,而析构的动作是在子线程当中执行的。

如果传递的是&a我们再来看下现象如何:

#include 
#include 
using namespace std;
class A {
public:
	A(int a) {
		std::cout << "构造函数执行了" << "this = " << this << " threadid = " << std::this_thread::get_id() << std::endl;
	}
	A(const A& a) {
		std::cout << "拷贝构造函数执行了" << "this = " << this << " threadid = " << std::this_thread::get_id() << std::endl;
	}
	~A() {
		std::cout << "析构函数执行了" << "this = " << this << " threadid = " << std::this_thread::get_id() << std::endl;
	}
public:
	void run(int ab) {
		std::cout << ab << std::endl;
		std::cout << "子线程ID = " << std::this_thread::get_id() <
						

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5718418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存