简体   繁体   English

在调用exit之后访问其他线程中的全局变量是否安全?

[英]Is it safe to access global variables in other thread after exit called?

I am analysing a dump one of the stack shows exit has been called 我正在分析一个转储,其中一个显示退出已被调用

#0  0x00007fe2beac80a4 in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007fe2beac3444 in _L_lock_1087 () from /lib64/libpthread.so.0
#2  0x00007fe2beac32b6 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x00007fe2bece5325 in _dl_fini () from /lib64/ld-linux-x86-64.so.2
#4  0x00007fe2bd73a5e5 in __run_exit_handlers () from /lib64/libc.so.6
#5  0x00007fe2bd73a635 in exit () from /lib64/libc.so.6

At the same time dump is in another thread when is trying to access global variable. 同时,在尝试访问全局变量时,转储在另一个线程中。 So is it safe to access global variable in another thread after exit has been called? 那么在调用exit之后在另一个线程中访问全局变量是否安全? Would the global variable be destroyed "any time" after exit called? 退出退出后,全局变量会“随时”销毁吗?

So is it safe to access global variable in another thread after exit has been called? 那么在调用exit之后在另一个线程中访问全局变量是否安全?

On any non-trivial OS, yes. 是的,在任何非平凡的操作系统上。 It's even safe to access local/automatic storage vars in other threads after exit has been called. 调用exit之后,甚至可以安全地访问其他线程中的本地/自动存储var。 Windows/linux etc. designers did think of this issue at design-time and so developed their process-termination sequence so that ALL threads in a process are stopped before any memory is released. Windows / linux等设计人员确实在设计时就想到了这个问题,因此开发了他们的进程终止序列,以便在释放任何内存之前先停止进程中的所有线程。 Process threads that are not in the running state have their state changed so that they can never be run again. 未处于运行状态的进程线程的状态已更改,因此它们将永远无法再次运行。 Process threads running on a different core than the thread that called 'exit' have their execution forcibly removed by means of a hardware interrupt to the core/s running them. 在与称为“退出”的线程不同的内核上运行的进程线程,通过对运行它们的内核的硬件中断来强制删除其执行。 Only when ALL threads are stopped are resources like memory deallocated and released for re-use by other processes. 只有当所有线程都停止时,才释放和释放资源(如内存),以供其他进程重新使用。

Would the global variable be destroyed "any time" after exit called? 退出退出后,全局变量会“随时”销毁吗?

Yes. 是。 The memory-segment hosting the global will be released by the OS at some time after exit is called. 调用退出后,操作系统将在某个时候释放托管全局内存的内存段。 This does not matter, however, since none of the process threads will be running at that time, whenever it is. 但这无关紧要,因为任何时候只要有进程线程都不会运行。

Note that all exit() calls are created equal:) Some will run static dtors etc. before calling the OS to terminate the process and this can indeed cause problems with threads that call them that may be still running, resulting in possible UB/segfaults. 请注意,所有exit()调用的创建方式均相同:)有些调用程序将在调用OS终止进程之前运行静态dtor等,这确实可能导致调用它们的线程可能仍在运行,从而可能导致UB / segfaults 。 This becomes more likely with non-POD classes with non-trivial dtors. 对于具有非平凡dtor的非POD类,这种情况更有可能出现。

If this is found to be an actual issue, either use an exit call that does not run the static dtors, (this may, or may not, matter, depending on what the dtor does, or needs to do), or avoid the issue by not using static globals, (not a bad plan anyway), or, as a last resort, go through the miserable, and usually avoidable, exercise of trying to cooperatively terminate the offending threads in user space before calling exit() . 如果发现这是一个实际问题,请使用不运行静态dtor的退出调用(这可能会也可能不会很重要,这取决于dtor的工作或需要执行的操作),或者避免该问题通过不使用静态全局变量(无论如何都不是一个坏计划),或者作为最后的选择,在调用exit()之前尝试在用户空间中共同终止有问题的线程,这是一个痛苦的过程,通常是可以避免的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 退出后从回调访问线程堆栈 - thread stack access from callback after exit Qt 线程执行在 exit() quit() 或 terminate() 被调用后不会停止 - Qt thread execution does not stop after exit() quit() or terminate() was called 使用c ++ 11的std :: thread,何时修改全局变量是线程安全的? - Using c++11's std::thread, when is modifying global variables thread-safe? 如何链接非线程安全库,以便每个线程都有自己的全局变量? - How to link non thread-safe library so each thread will have its own global variables from it? 在被另一个线程写入并在该线程加入之后从主线程访问变量是否安全? - Is it safe to access a variable from the main thread after it was written to by another thread and after that thread was joined? 创建具有独立线程的全局对象是否安全? - Is it safe to create a global object that has an independent thread? 线程安全访问成员变量 - Thread safe access to member variable 全局变量未在主线程中销毁? - Global variables not destructed in main thread? 线程调用的函数对删除对象安全吗? - Is function called by thread safe against object delete? 函数静态变量是否在GCC中是线程安全的? - Are function static variables thread-safe in GCC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM