C++11并发与多线程笔记(3)线程传参详解,detach()大坑,成员函数做线程函数

1、传递临时对象作为线程参数

1.1 要避免的陷阱1

#include <iostream>
#include <thread>
using namespace std;

void myPrint(const int &i, char* pmybuf)
{
	//如果线程从主线程detach了
	//i不是mvar真正的引用,实际上值传递,即使主线程运行完毕了,子线程用i仍然是安全的,但仍不推荐传递引用
	//推荐改为const int i
	cout << i << endl;
	//pmybuf还是指向原来的字符串,所以这么写是不安全的
	cout << pmybuf << endl;
}

int main()
{
	int mvar = 1;
	int& mvary = mvar;
	char mybuf[] = "this is a test";
	thread myThread(myPrint, mvar, mybuf);//第一个参数是函数名,后两个参数是函数的参数
	myThread.join();
	//myThread.detach();
	
	cout << "Hello World!" << endl;
}

在使用detach时,不推荐引用传递,指针传递肯定有问题

1.2 要避免的陷阱2

#include <iostream>
#include <thread>
#include <string>
using namespace std;

void myPrint(const int i, const string& pmybuf)
{
	cout << i << endl;
	cout << pmybuf << endl;
}

int main()
{
	int mvar = 1;
	int& mvary = mvar;
	char mybuf[] = "this is a test";
	//如果detach了,这样仍然是不安全的
	//因为存在主线程运行完了,mybuf被回收了,系统采用mybuf隐式类型转换成string
	//推荐先创建一个临时对象thread myThread(myPrint, mvar, string(mybuf));就绝对安全了
	thread myThread01(myPrint, mvar, mybuf);
	//myThread01.join();
	myThread01.detach();

	cout << "I love China!" << endl;
}

在创建线程的同时构造临时对象的方法传递参数是可行的

1.3 总结

  • 如果传递int这种基本数据类型,推荐使用值传递,不要用引用
  • 如果传递类对象,避免使用隐式类型转换,全部都是创建线程这一行就创建出临时对象,然后在函数参数里,用引用来接,否则还会创建出一个对象
  • 终极结论:建议不使用detach,只使用join,这样就不存在局部变量失效导致线程对内存的非法引用问题。

2、临时对象作为线程参数

2.1 线程id概念

  • id是个数字,每个线程(不管是主线程还是子线程)实际上都对应着一个数字,而且每个线程对应的这个数字都不一样。
  • 线程id可以用C++标准库里的函数来获取。**std::this_thread::get_id()**来获取

2.2 临时对象构造时机抓捕

#include<iostream>
#include<thread>
using namespace std;
class A {
public:
    int m_a;
	A(int a) :m_a(a) {
		cout << "A(int a):m_a(a)构造函数执行" << this << "  线程id为:" <<this_thread::get_id << endl;
	}
	A(const A& a):m_a(a.m_a){
		cout << "A(const A& a):m_a(a.m_a)拷贝构造函数执行" << this <<"  线程id为:" << this_thread::get_id << endl;
	}
	~A() {
		cout << "~A() 析构函数执行" << this <<"  线程id为:" << this_thread::get_id << endl;
	}

};
void myPrint(const int i, const A& pmybuf) {
	cout << i << endl;
	cout << pmybuf.m_a<< endl;
}
int main() {
	int mvar = 1;
	int myseconder = 12;
	cout<<"主线程id为:"<< this_thread::get_id() << endl;
	thread myThread01(myPrint, mvar, A(myseconder));
	myThread01.detach();
	cout << "hello world" << endl;
}

在这里插入图片描述

3、传递类对象、智能指针作为线程参数

3.1 类对象作为线程参数

#include <iostream>
#include <thread>
using namespace std;

class A {
public:
	mutable int m_i; //m_i即使实在const中也可以被修改
	A(int i) :m_i(i) {}
};

void myPrint(const A& pmybuf)//虽然是参数引用,但还是没有修改主线程中m_i值
{
	pmybuf.m_i = 199;//修改的值不影响main函数
	cout << "子线程myPrint的参数地址是" << &pmybuf << "thread = " << std::this_thread::get_id() << endl;
}

int main()
{
	A myObj(10);
	//myPrint(const A& pmybuf)中引用不能去掉,如果去掉会多创建一个对象
	//const也不能去掉,去掉会出错
	//即使是传递的const引用,但在子线程中还是会调用拷贝构造函数构造一个新的对象,
	//所以在子线程中修改m_i的值不会影响到主线程
	//如果希望子线程中修改m_i的值影响到主线程,可以用thread myThread(myPrint, std::ref(myObj));,省去了传递时的拷贝构造函数
	//这样const就是真的引用了,myPrint定义中的const就可以去掉了,类A定义中的mutable也可以去掉了
	thread myThread(myPrint, myObj);
	myThread.join();
	//myThread.detach();

	cout << "Hello World!" << endl;
}

分析:

  • void myPrint(const A& pmybuf)//虽然是参数引用(& pmybuf),但还是没有修改主线程中m_i值
  • 如果希望子线程中修改m_i的值影响到主线程,可以用thread myThread(myPrint, std::ref(myObj));

3.2 智能指针作为线程参数

#include <iostream>
#include <thread>
#include <memory>
using namespace std;

void myPrint(unique_ptr<int> ptn)
{
	cout << "thread = " << std::this_thread::get_id() << endl;
}

int main()
{
	unique_ptr<int> myp(new int(10));
	//独占式智能指针只能通过std::move()才可以传递给另一个指针
	//传递后up就指向空,新的ptn指向原来的内存
	//所以这时就不能用detach了,因为如果主线程先执行完,ptn指向的对象就被释放了
	thread myThread(myPrint, std::move(myp));
	myThread.join();
	//myThread.detach();

	return 0;
}

分析:

  • unique_ptr 构造函数已经将拷贝函数和拷贝赋值删除了,禁止拷贝,如果需要将unique_ptr对象传入,需要使用std::move(myp)

4、用成员函数指针做线程函数

class A {
public:
    int m_a;
	A(int a) :m_a(a) {
		cout << "A(int a):m_a(a)构造函数执行" << this << "  线程id为:" <<this_thread::get_id << endl;
	}
	A(const A& a):m_a(a.m_a){
		cout << "A(const A& a):m_a(a.m_a)拷贝构造函数执行" << this <<"  线程id为:" << this_thread::get_id << endl;
	}
	~A() {
		cout << "~A() 析构函数执行" << this <<"  线程id为:" << this_thread::get_id << endl;
	}
	
	void thread_work(int i) {
		cout << "子线程thread_work执行" << this <<"  thread_work线程id为:" << this_thread::get_id << endl;
	}
	void operator()(int i){
		cout << "子线程operator()执行" << this <<"  operator()线程id为:" << this_thread::get_id << endl;
	}
};

int main() {
	cout<<"主线程id为:"<< this_thread::get_id() << endl;
	A myobj(10);//生成一个类对象;
	//参数1:传成员函数地址
	//参数2:对象名
	//参数3:函数所需的参数
	thread myThread(&A::thread_work,myobj,15)
	// &myobj==std::ref(myobj) 不调用拷贝构造函数了,那后续如果调用myThread就不安全了
	//thread myThread(myobj,15) //使用 operator()作为子线程入口
	myThread.detach();
	cout << "hello world" << endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值