thread 构造函数原理:直接将参数原封不动地复制到子线程中然后就不管了。
如果main线程比子线程先结束,那么字符串被销毁,将给子线程带来灾难性后果。
所以不要在线程中传递 引用、指针 之类的参数。
错误写法一
void func(int i, char* str) {
cout << i << endl;
cout << str << endl;
}
int main() {
int mvar = 1;
char *str = "hello world";
std::thread myThread(func, mvar, str);
return 0;
}
错误写法二
那么是否可以这样写呢?
void func(int i, string& str) {
cout << i << endl;
cout << str << endl;
}
int main() {
int mvar = 1;
char *str = "hello world";
std::thread myThread(func, mvar, str);
return 0;
}
不可以,因为将 char* 转换为 string 类型时,发生了隐式类型转换,此时会自动调用 string(char *) 的构造函数生成一个 string 类型的临时对象。由于临时对象是不可以修改的,所以不能用 string& 引用临时对象,而应该使用 const string& 类型引用临时对象。
使用 const string& 是否就可以了呢?是否就安全了呢?
依然不安全,前面提到,发生隐式类型转换时,会自动调用 string(char*) 构造函数,但是调用时机是发生在 main 线程中还是发生在子线程中的呢?
调用时机是有可能发生在子线程中,那么这样就是不安全的,所以我们要保证这个调用时机一定是发生在 main 线程中。
证明:这种方式下调用构造函数的时机发生在子线程中
以下代码可以说明这个过程。由于无法修改 stl 中的 string 类,所以我们自己创建一个类并在其成员函数中添加打印信息在展示其成员函数的调用情况。
class Int {
public:
Int(int a) : m_i(a) { cout << "Int的构造函数调用,线程ID = " << std::this_thread::get_id() << endl; }
Int(const Int &s) { cout << "Int的拷贝构造函数调用,线程ID = " << std::this_thread::get_id() << endl; }
~Int() { cout << "Int的析构函数调用,线程ID = " << std::this_thread::get_id() << endl;
}
private:
int m_i;
};
void func(const Int &x) {
cout << "func函数调用,线程ID = " << std::this_thread::get_id() << endl;
}
int main() {
int mvar = 1;
cout << "main线程ID = " << std::this_thread::get_id() << endl;
std::thread myThread(func, mvar); //注意这里的第二个参数
myThread.join();
return 0;
}
运行结果:
从运行结果可以看到,将int类型隐式转换为Int类型发生在了子线程中,这样就会导致前面提到的致命性错误产生,即如果main线程比子线程先结束,则作为创建子线程的参数已经被销毁(例如参数是一个字符串),用已经被销毁的参数来创建子线程将导致不可预知的错误。
正确,安全的写法
在 main 线程中显式地将 char* 类型转换成 string 类型,然后将这个 string 对象传递给新线程。
void func(int i, const string& str) {
cout << i << endl;
cout << str << endl;
}
int main() {
int mvar = 1;
char *str = "hello world";
std::thread myThread(func, mvar, string(str)); //保证隐式类型转换发生在main线程中。
return 0;
}
虽然解决了这个问题,但是 func 函数的形参 str 引用的并不是隐式类型转换下生成的临时对象,而是 main 线程将这个临时对象复制到子线程中(调用拷贝构造函数)的新对象。
class Int {
public:
Int(int a) : m_i(a) { cout << "Int的构造函数调用,线程ID = " << std::this_thread::get_id() << endl; }
Int(const Int &s) { cout << "Int的拷贝构造函数调用,线程ID = " << std::this_thread::get_id() << endl; }
~Int() { cout << "Int的析构函数调用,线程ID = " << std::this_thread::get_id() << endl;
}
private:
int m_i;
};
void func(const Int &x) {
cout << "func函数调用,线程ID = " << std::this_thread::get_id() << endl;
}
int main() {
int mvar = 1;
cout << "main线程ID = " << std::this_thread::get_id() << endl;
std::thread myThread(func, Int(mvar)); //注意这里的第二个参数
myThread.join();
return 0;
}
运行结果:
从运行结果可以看到,Int的构造函数调用时机发生在主线程中,拷贝构造函数也发生在主线程中,这说明主线程安全地将数据传递到了子线程的手中,那么我们就不需要担心主线程的死活对子线程的影响了。