一、传递临时对象作为线程参数
1.1 要避免的陷阱1
#include<iostream>
#include<thread>
using namespace std;
void myprint(const int &i,char* pmybuf) {
cout << i << endl;
cout << pmybuf << endl;
return;
}
int main() {
int mvar = 6;
int &myvar = mvar;
char mybuf[] = "MD-DynamicDebug";
thread myobj(myprint,mvar,mybuf);
myobj.join();
cout << "主线程运行结束..." << endl;
system("pause");
return 0;
}
如果以detach方式创建线程
- mvar是否安全?
分析认为:i并不是mvar的引用,实际是值传递。那么我们认为,即便主线程detach子线程,那么子线程中用i值仍然是安全的
1.2 要避免的陷阱2
- mybuf是否安全
指针在detach子线程时,绝对会有问题
考虑传字符串解决方法解决
#include<iostream>
#include<thread>
using namespace std;
void myprint(const int i, const string& pmybuf) {
cout << i << endl;
cout << pmybuf.c_str() << endl;
return;
}
int main() {
int mvar = 6;
int &myvar = mvar;
char mybuf[] = "MD-DynamicDebug";
thread myobj(myprint, mvar, mybuf);
myobj.join();
cout << "主线程运行结束..." << endl;
system("pause");
return 0;
}
但是mybuf到底是在什么时候转换成string的
事实上存在mybuf被回收了(main函数执行完了),系统采用mybuf去转string的可能性
直接将mybuf转换成string对象,这是一个可以保证在线程中用肯定有效的对象
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。" << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。" << endl;
}
~A() {
cout << "析构函数执行。。" << endl;
}
private:
int m_i;
};
void myprint4(const int i, const A &pmybuf) {
cout << &pmybuf << endl;//这里打印的是pmybuf对象的
return;
}
int main() {
int mvar = 1;
int mysecondpar = 12;
thread myobj4(myprint4, mvar, mysecondpar);//我们希望mysecondpar转成A类型对象传递给myprint的第二个参数
myobj4.join();
cout << "主线程运行结束..." << endl;
system("pause");
return 0;
}
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << endl;
}
~A() {
cout << "析构函数执行。。。" << this << endl;
}
private:
int m_i;
};
void myprint4(const int i, const A &pmybuf) {
cout << &pmybuf << endl;//这里打印的是pmybuf对象的
return;
}
int main() {
int mvar = 1;
int mysecondpar = 12;
thread myobj4(myprint4, mvar, A(mysecondpar));
myobj4.detach();
cout << "主线程运行结束..." << endl;
system("pause");
return 0;
}
在创建线程的同时构造临时对象的方法传递参数是可行的
**事实1:**只要用临时构造的A类对象作为参数传递给线程,那么就一定能够在主线程执行完毕前把线程函数的第二个参数构建出来,从而确保即便detach,程序也能安全运行
1.3 总结
(a)如果传递int这种简单类型,建议都是值传递,不要用引用,防止节外生枝
(b)如果传递类对象,避免隐式类型转换。全部都是创建线程这一行就创建出临时对象,然后在函数参数里,用引用来接,否则系统还会构建出一个对象
(c)终极结论:建议不使用detach(),只使用join(),这样就不存在局部变量失效导致线程对内存的非法引用问题
二、临时对象作为线程参数继续讲
2.1 线程id概念
id是个数字,每个线程(不管是主线程还是子线程)实际上都对应着一个数字,而且每个线程对应的这个数字都不一样。也就是说不同的线程,它的线程id(数字)必然是不同的
线程id可以用C++标准库里的函数来获取。std::this_thread::get_id()来获取
2.2 临时对象构造时机捕获
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
private:
int m_i;
};
void myprint4(const int i, const A &pmybuf) {
cout << "&pmybuf=" << &pmybuf << endl;//这里打印的是pmybuf对象的
return;
}
int main() {
int mvar = 1;
int mysecondpar = 12;
//thread myobj4(myprint4, mvar, mysecondpar);//我们希望mysecondpar转成A类型对象传递给myprint的第二个参数
thread myobj4(myprint4, mvar, A(mysecondpar));
myobj4.detach();
cout << "主线程运行结束..." << endl;
system("pause");
return 0;
}
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
private:
int m_i;
};
void myprint6(const A &pmybuf) {
cout << "子线程myprint6的参数地址是:" << &pmybuf <<
"threadID=" << this_thread::get_id() << endl;
}
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
int mvar = 6;
thread myobj6(myprint6, mvar);
myobj6.join();
system("pause");
return 0;
}
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
private:
int m_i;
};
void myprint6(const A &pmybuf) {
cout << "子线程myprint6的参数地址是:" << &pmybuf <<
"threadID=" << this_thread::get_id() << endl;
}
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
int mvar = 6;
thread myobj6(myprint6, A(mvar));
myobj6.join();
system("pause");
return 0;
}
三、传递类对象、智能指针作为线程参数
在子线程中修改m_i的值不会影响到主线程?
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
mutable int m_i;
};
void myprint7(const A &pmybuf) {
pmybuf.m_i = 23;//我们修改该值,不会影响到main函数
cout << "子线程myprint6的参数地址是:" << &pmybuf <<
"threadID=" << this_thread::get_id() << endl;
}
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
A myobj7(19);//生成一个类对象
thread mythread(myprint7, myobj7);//myobj7将类对象作为线程参数
mythread.join();
system("pause");
return 0;
}
主线程值的地址和子线程中值的地址不同,所以在子线程中修改m_i的值不会影响到主线程
- 希望子线程中修改m_i的值影响到主线程
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
int m_i;//可以去掉mutable
};
//void myprint8(const A &pmybuf) {
void myprint8(A &pmybuf) {//const可以去掉
pmybuf.m_i = 23;//我们修改该值,不会影响到main函数
cout << "子线程myprint6的参数地址是:" << &pmybuf <<
"threadID=" << this_thread::get_id() << endl;
}
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
A myobj7(19);//生成一个类对象
thread mythread(myprint8, ref(myobj7));//希望子线程中修改m_i的值影响到主线程
mythread.join();
system("pause");
return 0;
}
#include<iostream>
#include<thread>
using namespace std;
void myprint9(unique_ptr<int> pzn) {
}
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
unique_ptr<int>myp(new int(100));
thread mythread(myprint9, move(myp));//希望子线程中修改m_i的值影响到主线程
mythread.join();//不可以用detach
system("pause");
return 0;
}
参数智能指针和原来的智能指针指向的是同一块内存
用成员函数指针做线程参数
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
void thread_work(int num) {
cout << "子线程thread_work执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
int m_i;
};
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
A myobj7(19);//生成一个类对象
thread mythread(&A::thread_work, myobj7, 25);
/*
thread mythread(&A::thread_work,ref(myobj7),25);//&myobj7==ref(myobj7)
thread mythread(&A::thread_work, &myobj7, 25);这两种情形用detach都不安全,没有复制出新对象
*/
mythread.join();
system("pause");
return 0;
}
#include<iostream>
#include<thread>
using namespace std;
class A {
public:
//类型转换构造函数,可以把一个int转换成一个类对象
A(int i) :m_i(i) {
cout << "构造函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
A(const A&a) :m_i(a.m_i) {
cout << "拷贝函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
~A() {
cout << "析构函数执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
void thread_work(int num) {
cout << "子线程thread_work执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
void operator()(int num) {
cout << "子线程()执行。。。" << this << " threadID:" << this_thread::get_id() << endl;
}
int m_i;
};
int main() {
cout << "主线程ID:" << this_thread::get_id() << endl;
A myobj7(19);//生成一个类对象
thread mythread(myobj7, 25);//可以用detach,调用了拷贝构造函数
//thread mythread(ref(myobj7), 25);//不调用拷贝构造函数
//thread mythread(&myobj7, 25);//报错
mythread.join();
system("pause");
return 0;
}