Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 677
Benoit Blanchon Avatar asked Sep 28 '15 13:09

Benoit Blanchon


People also ask

What is thread ID in C++?

std::thread:: id. The class thread::id is a lightweight, trivially copyable class that serves as a unique identifier of std::thread objects. Instances of this class may also hold the special distinct value that does not represent any thread. Once a thread has finished, the value of std::thread::id may be reused by another thread.

What is the difference between thread ID and thread ID rust?

ThreadId s are guaranteed not to be reused, even when a thread terminates. ThreadId s are under the control of Rust’s standard library and there may not be any relationship between ThreadId and the underlying platform’s notion of a thread identifier – the two concepts cannot, therefore, be used interchangeably.

Can thread ID be reused by another thread?

Once a thread has finished, the value of std::thread::id may be reused by another thread. This class is designed for use as key in associative containers, both ordered and unordered.

What is a threadId?

A unique identifier for a running thread. A ThreadId is an opaque object that uniquely identifies each thread created during the lifetime of a process. ThreadId s are guaranteed not to be reused, even when a thread terminates.


1 Answers

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.

like image 195
PSIAlt Avatar answered Sep 20 '22 13:09

PSIAlt