Skip to content

Commit 07191e2

Browse files
committed
Auto merge of #38548 - GuillaumeGomez:thread_struct_docs, r=frewsxcv
Add missing example for Thread struct r? @frewsxcv
2 parents 917e5ba + 3312feb commit 07191e2

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
@@ -648,6 +648,23 @@ pub fn park_timeout(dur: Duration) {
648648
/// A `ThreadId` is an opaque object that has a unique value for each thread
649649
/// that creates one. `ThreadId`s do not correspond to a thread's system-
650650
/// designated identifier.
651+
///
652+
/// # Examples
653+
///
654+
/// ```
655+
/// #![feature(thread_id)]
656+
///
657+
/// use std::thread;
658+
///
659+
/// let handler = thread::Builder::new()
660+
/// .spawn(|| {
661+
/// let thread = thread::current();
662+
/// let thread_id = thread.id();
663+
/// })
664+
/// .unwrap();
665+
///
666+
/// handler.join().unwrap();
667+
/// ```
651668
#[unstable(feature = "thread_id", issue = "21507")]
652669
#[derive(Eq, PartialEq, Copy, Clone)]
653670
pub struct ThreadId(u64);
@@ -700,6 +717,22 @@ struct Inner {
700717
#[derive(Clone)]
701718
#[stable(feature = "rust1", since = "1.0.0")]
702719
/// A handle to a thread.
720+
///
721+
/// # Examples
722+
///
723+
/// ```
724+
/// use std::thread;
725+
///
726+
/// let handler = thread::Builder::new()
727+
/// .name("foo".into())
728+
/// .spawn(|| {
729+
/// let thread = thread::current();
730+
/// println!("thread name: {}", thread.name().unwrap());
731+
/// })
732+
/// .unwrap();
733+
///
734+
/// handler.join().unwrap();
735+
/// ```
703736
pub struct Thread {
704737
inner: Arc<Inner>,
705738
}
@@ -723,6 +756,21 @@ impl Thread {
723756
/// Atomically makes the handle's token available if it is not already.
724757
///
725758
/// See the module doc for more detail.
759+
///
760+
/// # Examples
761+
///
762+
/// ```
763+
/// use std::thread;
764+
///
765+
/// let handler = thread::Builder::new()
766+
/// .spawn(|| {
767+
/// let thread = thread::current();
768+
/// thread.unpark();
769+
/// })
770+
/// .unwrap();
771+
///
772+
/// handler.join().unwrap();
773+
/// ```
726774
#[stable(feature = "rust1", since = "1.0.0")]
727775
pub fn unpark(&self) {
728776
let mut guard = self.inner.lock.lock().unwrap();
@@ -733,6 +781,23 @@ impl Thread {
733781
}
734782

735783
/// Gets the thread's unique identifier.
784+
///
785+
/// # Examples
786+
///
787+
/// ```
788+
/// #![feature(thread_id)]
789+
///
790+
/// use std::thread;
791+
///
792+
/// let handler = thread::Builder::new()
793+
/// .spawn(|| {
794+
/// let thread = thread::current();
795+
/// println!("thread id: {:?}", thread.id());
796+
/// })
797+
/// .unwrap();
798+
///
799+
/// handler.join().unwrap();
800+
/// ```
736801
#[unstable(feature = "thread_id", issue = "21507")]
737802
pub fn id(&self) -> ThreadId {
738803
self.inner.id

0 commit comments

Comments
 (0)