Is std :: thread :: id unique to all processes?

In my experience, it seems that the result

std::this_thread::get_id() 

unique in the process: identifiers differ from one process to another.

Is this guaranteed by the standard?

+7
c ++ 11 stl stdthread
source share
2 answers

std :: thread is implemented on top of pthreads in an environment that supports pthreads. Thus, it becomes no (portable) guarantee.

From the pthread_self guide:

Flow identifiers are guaranteed to be unique only within the process.
The thread identifier may be reused after attaching the completed thread or the disconnected thread has stopped.

+1
source share

The standard grantees, which are thread identifiers, are unique across streams, he also says that completed stream identifiers can be reused. It does not define processes and does not recognize their existence; therefore, it does not guarantee the uniqueness of processes.

30.3.1.1

  • An object of type thread :: id provides a unique identifier for each thread of execution and one separate value for all objects of the thread that do not represent a thread of execution (30.3.1). Each thread of execution has an associated thread :: id object, which is not equal to thread :: id of the object of any other thread of execution, and this is not equal to the thread :: id object of any std :: thread object that thread threads do not represent.
  • thread :: id should be a copyable class (section 9). The library can reuse the value of the stream :: id of the final thread that can no longer be combined.

The standard also hides the implementation of the :: id stream, it could be int or something else.

+2
source share

All Articles