Skip to content

Commit cf8e1fe

Browse files
add a simple example for thread::current()
1 parent 528c6f3 commit cf8e1fe

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libstd/thread/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ pub fn spawn<F, T>(f: F) -> JoinHandle<T> where
322322
}
323323

324324
/// Gets a handle to the thread that invokes it.
325+
///
326+
/// #Examples
327+
///
328+
/// Getting a handle to the current thread with `thread::current()`:
329+
///
330+
/// ```
331+
/// use std::thread;
332+
///
333+
/// let handler = thread::Builder::new()
334+
/// .name("named thread".into())
335+
/// .spawn(|| {
336+
/// let handle = thread::current();
337+
/// assert_eq!(handle.name(), Some("named thread"));
338+
/// })
339+
/// .unwrap();
340+
///
341+
/// handler.join().unwrap();
342+
/// ```
325343
#[stable(feature = "rust1", since = "1.0.0")]
326344
pub fn current() -> Thread {
327345
thread_info::current_thread().expect("use of std::thread::current() is not \

0 commit comments

Comments
 (0)