Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
主线程退出后线程访问共享变量 如果在线程退出和破坏共享变量后,分离线程访问共享变量(例如全局变量),那么多线程C++程序会发生什么? class A { public: A() { printf("Constructing A\n"); } ~A() { printf("Destructing A\n"); } void printSomething() { printf("A is printing\n"); } } A a; void thread_func() { printf("begin thread.\n"); sleep(3); // make sure main thread exit first a.printSomething(); printf("ending thread"); } int main(int argc, char** argv) { std::thread t(thread_func); t.detach(); return 0; }_C++_Multithreading_Detach - Fatal编程技术网

主线程退出后线程访问共享变量 如果在线程退出和破坏共享变量后,分离线程访问共享变量(例如全局变量),那么多线程C++程序会发生什么? class A { public: A() { printf("Constructing A\n"); } ~A() { printf("Destructing A\n"); } void printSomething() { printf("A is printing\n"); } } A a; void thread_func() { printf("begin thread.\n"); sleep(3); // make sure main thread exit first a.printSomething(); printf("ending thread"); } int main(int argc, char** argv) { std::thread t(thread_func); t.detach(); return 0; }

主线程退出后线程访问共享变量 如果在线程退出和破坏共享变量后,分离线程访问共享变量(例如全局变量),那么多线程C++程序会发生什么? class A { public: A() { printf("Constructing A\n"); } ~A() { printf("Destructing A\n"); } void printSomething() { printf("A is printing\n"); } } A a; void thread_func() { printf("begin thread.\n"); sleep(3); // make sure main thread exit first a.printSomething(); printf("ending thread"); } int main(int argc, char** argv) { std::thread t(thread_func); t.detach(); return 0; },c++,multithreading,detach,C++,Multithreading,Detach,该计划产生: bash$ ./a.out Constructing A Destructing A bash$ 似乎主线程创建了全局变量a,并在退出时将其销毁。那么,如果分离的子线程尝试访问这个全局变量,3秒钟后会发生什么 class A { public: A() { printf("Constructing A\n"); } ~A() { printf("Destructing A\n"); } void printSomething() { printf("A is pri

该计划产生:

bash$ ./a.out
Constructing A
Destructing A
bash$
似乎主线程创建了全局变量a,并在退出时将其销毁。那么,如果分离的子线程尝试访问这个全局变量,3秒钟后会发生什么

class A {
 public:
  A() { printf("Constructing A\n"); }
  ~A() { printf("Destructing A\n"); }
  void printSomething() { printf("A is printing\n"); }
}

A a;

void thread_func() {
  printf("begin thread.\n");
  sleep(3); // make sure main thread exit first
  a.printSomething();
  printf("ending thread");
}

int main(int argc, char** argv) {
  std::thread t(thread_func);
  t.detach();

  return 0;
}

另一个困惑是,为什么主线程在退出时会清除所有资源?看起来全局变量的生存期仅取决于主线程?

线程本身没有自己的内存,但与父进程共享内存。他们与父母有联系;因此,每当父线程死亡时,它的子线程也会被终止

进程在
main()
返回时退出,或者任何线程调用
exit()
\u exit()
时退出

但是,
main()
可以调用
pthread\u exit()
——这将不会终止进程。根据Linux:

当线程终止时,处理共享资源(例如互斥锁、, 条件变量、信号量和文件描述符)不可用 不调用使用atexit(3)注册的函数

进程中的最后一个线程终止后,进程终止 通过调用退出状态为零的退出(3);因此 使用发布进程共享资源并注册函数 atexit(3)被称为


当主线程退出时,它也将破坏t。如果希望某些内容比main更长寿,请使用fork。线程函数中的
printf()
语句在哪里?printf()语句不会每次都在shell中输出。有时会打印出“开始线程”。我认为这取决于两个线程的调度顺序。即使子线程被分离,它也会在主线程退出时被终止?因此,分离一个线程不会从父线程移除它的依赖关系。当您分离一个线程时,它的父线程不再需要杀死它来清理资源。分离的线程一旦退出,将自动清理它们的内存,而不需要父线程加入它们。根据定义,线程不能作为自己的实体存在。它们必须是过程的一部分。