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:: 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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With