Skip to content

Commit 14481f7

Browse files
committedApr 12, 2017
Auto merge of #41008 - sagebind:thread_id, r=alexcrichton
Derive Hash for ThreadId + better example Derive `Hash` for `ThreadId` (see comments in #21507). Useful for making maps based on thread, e.g. `HashMap<ThreadId, ?>`. Also update example code for thread IDs to be more useful.
2 parents 910c481 + cd14a32 commit 14481f7

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed
 

‎src/libstd/thread/mod.rs

+13-24
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(Eq, PartialEq, Clone, Copy, Hash, Debug)]
676674
pub struct ThreadId(u64);
677675

678676
impl ThreadId {
@@ -701,13 +699,6 @@ impl ThreadId {
701699
}
702700
}
703701

704-
#[unstable(feature = "thread_id", issue = "21507")]
705-
impl fmt::Debug for ThreadId {
706-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
707-
f.pad("ThreadId { .. }")
708-
}
709-
}
710-
711702
////////////////////////////////////////////////////////////////////////////////
712703
// Thread
713704
////////////////////////////////////////////////////////////////////////////////
@@ -795,14 +786,12 @@ impl Thread {
795786
///
796787
/// use std::thread;
797788
///
798-
/// let handler = thread::Builder::new()
799-
/// .spawn(|| {
800-
/// let thread = thread::current();
801-
/// println!("thread id: {:?}", thread.id());
802-
/// })
803-
/// .unwrap();
789+
/// let other_thread = thread::spawn(|| {
790+
/// thread::current().id()
791+
/// });
804792
///
805-
/// handler.join().unwrap();
793+
/// let other_thread_id = other_thread.join().unwrap();
794+
/// assert!(thread::current().id() != other_thread_id);
806795
/// ```
807796
#[unstable(feature = "thread_id", issue = "21507")]
808797
pub fn id(&self) -> ThreadId {

0 commit comments

Comments
 (0)
Please sign in to comment.