线程传参详解,detach()大坑,成员函数做线程函数
我的实践
#include <iostream>
#include <thread>
using namespace std;
//1.有问题的多线程函数
/*
void print(const int &i, char *p){
cout<< i <<endl;
cout<< p <<endl;
}
*/
//2.正解多线程函数
/*
void print(const int &i, const string &p){//在多线程中除了指针 无论引用还是值传递,都只会发生值传递,但是我们应该用引用,不然会拷贝两次
cout<< i <<endl; //这是因为线程创建本身就会发生拷贝, 然后线程调用函数又会拷贝一次// 有待验证
cout<< p <<endl;
}
*/
//3.对象验证
class T{
public:
int m;
public:
T(int a){
m = a;
cout<<"构造函数调用"<<"线程Id="<<this_thread::get_id()<<endl;
}
T(const T &ori){
this->m = ori.m;
cout<<"拷贝构造函数调用"<<"线程Id="<<this_thread::get_id()<<endl;
}
~T(){
cout<<"析构函数被调用"<<"线程Id="<<this_thread::get_id()<<endl;
}
};
void print(const int &i, const T& a){//用引用T &a,不然就会多一次拷贝构造函数。
cout<<i<<"线程Id="<<this_thread::get_id()<<endl;
cout<<a.m<<"线程Id="<<this_thread::get_id()<<endl;
}
int main(){
/******detach()大坑 1 *******
指针s指向的地址一致,主线程结束,s销毁,其他线程会出现问题。
****************************/
//int a = 1;
//char s[] = "多线程";
//thread myThread(print, a, s);
//myThread.detach();
/******detach()大坑 2 *******
把print函数的char* p改成string &p, 这样在传参的过程中会发生隐式类型转化。
地址就不一致了,这样即使主线程结束也不会影响。
但是仍然存在线程还没来得及创建,主线程就结束的情况,这仍然不行。
*****************************/
/******3 detach()正解之 临时对象 *******
临时对象会保证线程一定创建,并且线程会将对象复制一遍。
如下:
****************************/
//thread myThread(print, a, string(s));
//myThread.detach();
//下面用类T来验证,是否成功构造了对象,并且调用了对象的哪些函数,
int i = 1; int b = 2;
thread myThread(print, i, T(b) );//T(b)临时对象
myThread.detach();
return 0;
/*
输出如下:
构造函数调用线程Id=1 //在主线程中构造,已经构造完成,main函数结束也不影响
拷贝构造函数调用线程Id=1
拷贝构造函数调用线程Id=1
析构函数被调用线程Id=1
析构函数被调用线程Id=1
1线程Id=2
2线程Id=2
析构函数被调用线程Id=2
*/
}
一、传递临时对象作为线程参数
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;
}
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 myThread(myPrint, mvar, mybuf);
myThread.join();
//myThread.detach();
cout << "Hello World!" << endl;
}
1.3总结
- 如果传递int这种简单类型,推荐使用值传递,不要用引用
- 如果传递类对象,避免使用隐式类型转换,全部都是创建线程这一行就创建出临时对象,然后在函数参数里,用引用来接,否则还会创建出一个对象
- 终极结论:建议不使用detach
二、临时对象作为线程参数继续讲
2.1线程id概念
- id是个数字,每个线程(不管是主线程还是子线程)实际上都对应着一个数字,而且每个线程对应的这个数字都不一样
- 线程id可以用C++标准库里的函数来获取。std::this_thread::get_id()来获取
三、传递类对象、智能指针作为线程参数
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 thread_work(int num){
cout<<"子线程执行"<<endl;
}
};
void myPrint(const A& pmybuf)
{
pmybuf.m_i = 199;
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;
}
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> up(new int(10));
//独占式指针只能通过std::move()才可以传递给另一个指针
//传递后up就指向空,新的ptn指向原来的内存
//所以这时就不能用detach了,因为如果主线程先执行完,ptn指向的对象就被释放了
thread myThread(myPrint, std::move(up));
myThread.join();
//myThread.detach();
return 0;
}
4. 成员函数指针做线程函数
int main()
{
A myobj(10);//生成一个类对象
//std::thread mythread(&A::thread_work, myobj, 15);//分别是 成员函数地址,对象名的引用,参数
std::thread mythread(&A::thread_work, std::ref(myobj), 15);
mythread.join();
return 0;
}