将cha*类型字符串作为thread的参数如何保证安全

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的构造函数调用时机发生在主线程中,拷贝构造函数也发生在主线程中,这说明主线程安全地将数据传递到了子线程的手中,那么我们就不需要担心主线程的死活对子线程的影响了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值