一、传递类对象作为线程参数

#include<iostream>
#include<thread>

using namespace std;

class A
{
public:
	mutable int m_i;

	//类型转换构造函数,可以把一个int转换成一个类A对象
	A(int a) :m_i(a) {
		cout << "[A::A(int a)构造函数执行]" << this << "thread_id = " << std::this_thread::get_id() << endl;
	}


	A(const A& a) :m_i(a.m_i) {
		cout << "[A::A(int a)拷贝构造函数执行]" << this << "thread_id = " << std::this_thread::get_id() << endl;
	}

	~A() {
		cout << "[A::~A(int a)析构函数执行]" << this << "thread_id = " << std::this_thread::get_id() << endl;
	}
};


void myprint2(const A& pmybuf)
{
	pmybuf.m_i = 199; // 因为是mutable的,修改该值不会影响main函数
	cout << "子线程myprint2的参数地址是" << &pmybuf << "thread_id = " << std::this_thread::get_id() << endl;// 打
10-25 08:29