c/c++ 多线程 detach的困惑

多线程 detach的困惑

求大神解答:

1,当在一个函数里启动一个线程后,并detach了

2,detach的线程里使用了这个函数里new出来的一个对象

3,detach后,delete了这个对象

4,为什么detach在线程里,使用了在3处delete的内存还不报错误???

-----start 更新分割线2018/10/27 上午-------------
回答4的问题:

线程还没来得及执行,main函数就执行完了,直接杀死还没有执行完的线程,所以线程里使用了已经delete的内存,也没有出错。如果在main函数里调用sleep(2),就会出错误。

如果当main函数结束后,还不想结束其他由main函数创建的子线程,就必须调用下pthread_exit(NULL)。

#include <iostream>
#include <thread>
#include <unistd.h>

using namespace std;

class bad{
public:
  bad(int* i) : data(i){
  cout << "addr2:" << data << endl;
  }
  void operator()(){
    for(unsigned j = 0; j < 10000000000; ++j){
      something(data);
    }
  }
private:
  void something(int* i){
    *i = 100;
    cout << *i << endl;
  };
  int* data;
};

void func(){
  int* local = new int(10);
  cout << "addr1:" << local << endl;
  bad b(local);
  delete local;
  thread t(b);
  //cout << "before join " << *local << endl;
  cout << "end delete" << endl;
  t.detach();
  //t.join();

  cout << "after join " << *local << endl;
  cout << "func end" << endl;
}
int main(){
  func();
  pthread_exit(NULL);
  cout << "end" << endl;
}

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

原文地址:https://www.cnblogs.com/xiaoshiwang/p/9847013.html