Pthread在Linux中的生命周期 [英] Pthread lifetimes in linux
问题描述
我自己的测试以及众多的Web资源显示,当主线程退出或被杀死时,所有分离的子线程都会死亡(除非它以 pthread_exit
退出)。
c - 主线程退出,其他退出也是? - 堆栈溢出 [ ^ ]
我的问题是,这是否一直是指定的行为?或者旧的可能有所不同(10年以上)嵌入式Linux版本?
谢谢
PS
我问的原因是我正在深入研究一些做出相反假设的遗留代码。我想知道原作者是不是错了,或者我错过了什么。
我尝试了什么:
我没有发现任何(历史上)不同的行为。
My own tests, as well as numerous web sources, show that all detached child threads die when the main thread exits or is killed (unless it exits with pthread_exit
).
c - main thread exit, does other exit too? - Stack Overflow[^]
My question is, has this always been the specified behaviour? or could it have been different in old (10+ years) embedded Linux versions?
Thanks
PS
The reason for my asking is that I am digging into some legacy code that makes the opposite assumption. I want to know if the original writer was mistaken, or if I have missed something.
What I have tried:
I have not found anything an a (historically) different behavior.
推荐答案
一个有趣的问题。我没有最终的答案,但有些希望是有用的信息。
实际行为由POSIX标准定义(并且总是如此)。
但Linux内核在内部使用不同的线程模型。因此必须进行更改以提供完整的pthread兼容性。请参阅2002年的PDF: POSIX线程和Linux内核 [< a href =https://www.kernel.org/doc/ols/2002/ols2002-pages-330-337.pdftarget =_ blanktitle =New Window> ^ ]。
这些更改已在内核版本2.6中实现(请参阅原生POSIX线程图书馆 - 维基百科 [ ^ ])。
如此老旧的内核使用 LinuxThreads - Wikipedia [ ^ ]这些不兼容POSIX,并且可能在子线程方面表现不同(不存在LinuxThreads模型使用所谓的任务)。
An interesting question. I have not a final answer but some hopefully useful information.
The actual behaviour is defined by the POSIX standard (and was so always).
But the Linux kernel uses a different thread model internally. So it had to be changed to provide full pthread compatibilty. See this PDF from 2002: POSIX Threads and the Linux Kernel[^].
The changes has been implemented with kernel version 2.6 (see Native POSIX Thread Library - Wikipedia[^]).
So older kernels used LinuxThreads - Wikipedia[^] which are not POSIX compatible and might behave differently regarding child threads (which did not exist with the LinuxThreads model that uses so called "tasks").
这篇关于Pthread在Linux中的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!