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

From my experience, it seems that the result of

std::this_thread::get_id()

is unique across process: ids are different from one process to another.

Is this guaranteed by the standard?


std::thread is implemented on top of pthreads in an environment supporting pthreads. So its becomes there is no (portable) guarantee.

From pthread_self manual:

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.


The standard grantees that thread ids are unique across different threads, it also says that terminated thread ids might be reused. It doesn't specify processes, and doesn't acknowledged their existence, so, therefore, it doesn't guarantee uniqueness across processes.

30.3.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.
  • 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.
  • The standard also hides away the implementation of a thread::id, it could be a int or something else.

    链接地址: http://www.djcxy.com/p/29456.html

    上一篇: 使用Artifactory防止覆盖部署

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