Skip to content

Commit c655348

Browse files
author
Felix Raimundo
committed
Add more examples to thread::spawn
Part of #29378
1 parent ced823e commit c655348

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

src/libstd/thread/mod.rs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ impl Builder {
315315
/// thread finishes). The join handle can be used to block on
316316
/// termination of the child thread, including recovering its panics.
317317
///
318+
/// For a more complete documentation see [`thread::spawn`][`spawn`].
319+
///
318320
/// # Errors
319321
///
320322
/// Unlike the [`spawn`] free function, this method yields an
@@ -392,14 +394,10 @@ impl Builder {
392394
/// Panics if the OS fails to create a thread; use [`Builder::spawn`]
393395
/// to recover from such errors.
394396
///
395-
/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
396-
/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
397-
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
398-
/// [`panic`]: ../../std/macro.panic.html
399-
/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
400-
///
401397
/// # Examples
402398
///
399+
/// Simple thread creation.
400+
///
403401
/// ```
404402
/// use std::thread;
405403
///
@@ -409,6 +407,53 @@ impl Builder {
409407
///
410408
/// handler.join().unwrap();
411409
/// ```
410+
///
411+
/// As mentionned in the module documentation, threads are usualy made to
412+
/// communicate using [`channel`s][`channels`], here is how it usually looks.
413+
///
414+
/// This example also shows how to use `move`, in order to give ownership
415+
/// of values to a thread.
416+
///
417+
/// ```
418+
/// use std::thread;
419+
/// use std::sync::mpsc::channel;
420+
///
421+
/// let (tx, rx) = channel();
422+
///
423+
/// let sender = thread::spawn(move || {
424+
/// tx.send("Hello, thread".to_owned());
425+
/// });
426+
///
427+
/// let receiver = thread::spawn(move || {
428+
/// println!("{}", rx.recv().unwrap());
429+
/// });
430+
///
431+
/// sender.join();
432+
/// receiver.join();
433+
/// ```
434+
///
435+
/// A thread can also return a value through its [`JoinHandle`], you can use
436+
/// this to make asynchronous computations (futures might be more appropriate
437+
/// though).
438+
///
439+
/// ```
440+
/// use std::thread;
441+
///
442+
/// let computation = thread::spawn(|| {
443+
/// // Some expensive computation.
444+
/// 42
445+
/// });
446+
///
447+
/// let result = computation.join().unwrap();
448+
/// println!("{}", v);
449+
/// ```
450+
///
451+
/// [`channels`]: ../../std/sync/mpsc/index.html
452+
/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
453+
/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
454+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
455+
/// [`panic`]: ../../std/macro.panic.html
456+
/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
412457
#[stable(feature = "rust1", since = "1.0.0")]
413458
pub fn spawn<F, T>(f: F) -> JoinHandle<T> where
414459
F: FnOnce() -> T, F: Send + 'static, T: Send + 'static

0 commit comments

Comments
 (0)