std::thread::id 跨进程是否唯一?

Is std::thread::id unique across processes?

根据我的经验,似乎

的结果
std::this_thread::get_id()

跨进程唯一:进程与进程之间的 ID 不同。

这个有标准保证吗?

std::thread 在支持 pthreads 的环境中在 pthreads 之上实现。所以它变得没有(便携式)保证。

来自 pthread_self 手册:

Thread IDs are guaranteed to be unique only within a process. A
thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated.

线程 ID 在不同线程中是唯一的标准受让人,它还表示终止的线程 ID 可能会被重用。它不指定流程,也不承认它们的存在,因此,它不保证跨流程的唯一性。

30.3.1.1

  1. An object of type thread::id provides a unique identifier for each thread of execution and a single distinct value for all thread objects that do not represent a thread of execution (30.3.1). Each thread of execution has an associated thread::id object that is not equal to the thread::id object of any other thread of execution and that is not equal to the thread::id object of any std::thread object that does not represent threads of execution.
  2. thread::id shall be a trivially copyable class (Clause 9). The library may reuse the value of a thread::id of a terminated thread that can no longer be joined.

该标准还隐藏了 thread::id 的实现,它可以是 int 或其他东西。