Linux线程ID的回收策略是什么?
除非新的 PID 达到最大限制并被倒带,否则 Linux 进程 ID 不会立即被重用。
当我使用 pthread_self() 获取线程 ID 时,我得到了 1028、1034 之类的 TID。我猜这是进程中线程的内部“序列号”。所以我想使用像PID回收策略这样的线程id回收策略会更合适。 但我不太确定它是否适用于 Linux pthread 实现。
最佳答案
线程化的 linux 进程有
- 进程内所有线程共享的 OS pid - 使用
getpid
- 进程中的每个线程都有自己的操作系统线程 ID - 使用
gettid
- pthreads 内部使用的 pthreads 线程 ID,用于在进行各种 pthread 相关调用时识别线程 - 使用
pthread_self
和类似的。
如果您尝试实现“回收策略”或您认为需要这样做的原因,则无法从您的问题中确定。
编辑
出于好奇,您可以查看 linux pthread 代码,但从技术上讲您没有理由关心。 POSIX 规范基本上只是说线程 ID 保证在进程中是唯一的,并且在线程死亡后可以自由重用。
Although implementations may have thread IDs that are unique in a system, applications should only assume that thread IDs are usable and unique within a single process. The effect of calling any of the functions defined in this volume of IEEE Std 1003.1-2001 and passing as an argument the thread ID of a thread from another process is unspecified. A conforming implementation is free to reuse a thread ID after the thread terminates if it was created with the detachstate attribute set to PTHREAD_CREATE_DETACHED or if pthread_detach() or pthread_join() has been called for that thread.
关于c - Linux线程id回收策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22116012/