Skip to content

Commit 2820295

Browse files
committed
Derive Hash for ThreadId + better example
1 parent 5e122f5 commit 2820295

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/libstd/thread/mod.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ pub fn park_timeout(dur: Duration) {
652652
/// A unique identifier for a running thread.
653653
///
654654
/// A `ThreadId` is an opaque object that has a unique value for each thread
655-
/// that creates one. `ThreadId`s do not correspond to a thread's system-
656-
/// designated identifier.
655+
/// that creates one. `ThreadId`s are not guaranteed to correspond to a thread's
656+
/// system-designated identifier.
657657
///
658658
/// # Examples
659659
///
@@ -662,17 +662,15 @@ pub fn park_timeout(dur: Duration) {
662662
///
663663
/// use std::thread;
664664
///
665-
/// let handler = thread::Builder::new()
666-
/// .spawn(|| {
667-
/// let thread = thread::current();
668-
/// let thread_id = thread.id();
669-
/// })
670-
/// .unwrap();
665+
/// let other_thread = thread::spawn(|| {
666+
/// thread::current().id()
667+
/// });
671668
///
672-
/// handler.join().unwrap();
669+
/// let other_thread_id = other_thread.join().unwrap();
670+
/// assert!(thread::current().id() != other_thread_id);
673671
/// ```
674672
#[unstable(feature = "thread_id", issue = "21507")]
675-
#[derive(Eq, PartialEq, Copy, Clone)]
673+
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
676674
pub struct ThreadId(u64);
677675

678676
impl ThreadId {
@@ -795,14 +793,12 @@ impl Thread {
795793
///
796794
/// use std::thread;
797795
///
798-
/// let handler = thread::Builder::new()
799-
/// .spawn(|| {
800-
/// let thread = thread::current();
801-
/// println!("thread id: {:?}", thread.id());
802-
/// })
803-
/// .unwrap();
796+
/// let other_thread = thread::spawn(|| {
797+
/// thread::current().id()
798+
/// });
804799
///
805-
/// handler.join().unwrap();
800+
/// let other_thread_id = other_thread.join().unwrap();
801+
/// assert!(thread::current().id() != other_thread_id);
806802
/// ```
807803
#[unstable(feature = "thread_id", issue = "21507")]
808804
pub fn id(&self) -> ThreadId {

0 commit comments

Comments
 (0)