Skip to content

Commit 8d8f699

Browse files
committed
Clarify the guarantees that ThreadId does and doesn't make.
The existing documentation does not spell out whether `ThreadId`s are unique during the lifetime of a thread or of a process. I had to examine the source code to realise (pleasingly!) that they're unique for the lifetime of a process. That seems worth documenting clearly, as it's a strong guarantee. Examining the way `ThreadId`s are created also made me realise that the `as_u64` method on `ThreadId` could be a trap for the unwary on those platforms where the platform's notion of a thread identifier is also a 64 bit integer (particularly if they happen to use a similar identifier scheme to `ThreadId`). I therefore think it's worth being even clearer that there's no relationship between the two.
1 parent 28b948f commit 8d8f699

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

library/std/src/thread/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,13 @@ pub fn park_timeout(dur: Duration) {
965965

966966
/// A unique identifier for a running thread.
967967
///
968-
/// A `ThreadId` is an opaque object that has a unique value for each thread
969-
/// that creates one. `ThreadId`s are not guaranteed to correspond to a thread's
970-
/// system-designated identifier. A `ThreadId` can be retrieved from the [`id`]
971-
/// method on a [`Thread`].
968+
/// A `ThreadId` is an opaque object that uniquely identifies each thread
969+
/// created during the lifetime of a process. `ThreadId`s are guaranteed not to
970+
/// be reused, even if a thread dies. `ThreadId`s are under the control of
971+
/// Rust's standard library and there may not be any relationship between
972+
/// `ThreadId` and the underlying platform's notion of a thread identifier --
973+
/// the two concepts cannot, therefore, be used interchangeably. A `ThreadId`
974+
/// can be retrieved from the [`id`] method on a [`Thread`].
972975
///
973976
/// # Examples
974977
///

0 commit comments

Comments
 (0)