Skip to content

Commit 3312feb

Browse files
Add missing example for Thread struct
1 parent 1b38776 commit 3312feb

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/libstd/thread/mod.rs

+65
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,23 @@ pub fn park_timeout(dur: Duration) {
558558
/// A `ThreadId` is an opaque object that has a unique value for each thread
559559
/// that creates one. `ThreadId`s do not correspond to a thread's system-
560560
/// designated identifier.
561+
///
562+
/// # Examples
563+
///
564+
/// ```
565+
/// #![feature(thread_id)]
566+
///
567+
/// use std::thread;
568+
///
569+
/// let handler = thread::Builder::new()
570+
/// .spawn(|| {
571+
/// let thread = thread::current();
572+
/// let thread_id = thread.id();
573+
/// })
574+
/// .unwrap();
575+
///
576+
/// handler.join().unwrap();
577+
/// ```
561578
#[unstable(feature = "thread_id", issue = "21507")]
562579
#[derive(Eq, PartialEq, Copy, Clone)]
563580
pub struct ThreadId(u64);
@@ -610,6 +627,22 @@ struct Inner {
610627
#[derive(Clone)]
611628
#[stable(feature = "rust1", since = "1.0.0")]
612629
/// A handle to a thread.
630+
///
631+
/// # Examples
632+
///
633+
/// ```
634+
/// use std::thread;
635+
///
636+
/// let handler = thread::Builder::new()
637+
/// .name("foo".into())
638+
/// .spawn(|| {
639+
/// let thread = thread::current();
640+
/// println!("thread name: {}", thread.name().unwrap());
641+
/// })
642+
/// .unwrap();
643+
///
644+
/// handler.join().unwrap();
645+
/// ```
613646
pub struct Thread {
614647
inner: Arc<Inner>,
615648
}
@@ -633,6 +666,21 @@ impl Thread {
633666
/// Atomically makes the handle's token available if it is not already.
634667
///
635668
/// See the module doc for more detail.
669+
///
670+
/// # Examples
671+
///
672+
/// ```
673+
/// use std::thread;
674+
///
675+
/// let handler = thread::Builder::new()
676+
/// .spawn(|| {
677+
/// let thread = thread::current();
678+
/// thread.unpark();
679+
/// })
680+
/// .unwrap();
681+
///
682+
/// handler.join().unwrap();
683+
/// ```
636684
#[stable(feature = "rust1", since = "1.0.0")]
637685
pub fn unpark(&self) {
638686
let mut guard = self.inner.lock.lock().unwrap();
@@ -643,6 +691,23 @@ impl Thread {
643691
}
644692

645693
/// Gets the thread's unique identifier.
694+
///
695+
/// # Examples
696+
///
697+
/// ```
698+
/// #![feature(thread_id)]
699+
///
700+
/// use std::thread;
701+
///
702+
/// let handler = thread::Builder::new()
703+
/// .spawn(|| {
704+
/// let thread = thread::current();
705+
/// println!("thread id: {:?}", thread.id());
706+
/// })
707+
/// .unwrap();
708+
///
709+
/// handler.join().unwrap();
710+
/// ```
646711
#[unstable(feature = "thread_id", issue = "21507")]
647712
pub fn id(&self) -> ThreadId {
648713
self.inner.id

0 commit comments

Comments
 (0)