diff options
| author | Felix Raimundo <felix.raimundo@tweag.io> | 2017-05-09 13:20:04 +0200 |
|---|---|---|
| committer | Felix Raimundo <felix.raimundo@tweag.io> | 2017-05-09 13:20:04 +0200 |
| commit | c655348f26a179193f5d6d1a3a2fbe000a046699 (patch) | |
| tree | 3d3abf5d6de20a82bbae6182771db8f570417af7 /src/libstd/thread | |
| parent | ced823e267c132fab172b1890b24073995e79ffa (diff) | |
| download | rust-c655348f26a179193f5d6d1a3a2fbe000a046699.tar.gz rust-c655348f26a179193f5d6d1a3a2fbe000a046699.zip | |
Add more examples to `thread::spawn`
Part of #29378
Diffstat (limited to 'src/libstd/thread')
| -rw-r--r-- | src/libstd/thread/mod.rs | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 4cbcfdbc2d7..c1e894510b9 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -315,6 +315,8 @@ impl Builder { /// thread finishes). The join handle can be used to block on /// termination of the child thread, including recovering its panics. /// + /// For a more complete documentation see [`thread::spawn`][`spawn`]. + /// /// # Errors /// /// Unlike the [`spawn`] free function, this method yields an @@ -392,14 +394,10 @@ impl Builder { /// Panics if the OS fails to create a thread; use [`Builder::spawn`] /// to recover from such errors. /// -/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html -/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join -/// [`Err`]: ../../std/result/enum.Result.html#variant.Err -/// [`panic`]: ../../std/macro.panic.html -/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn -/// /// # Examples /// +/// Simple thread creation. +/// /// ``` /// use std::thread; /// @@ -409,6 +407,53 @@ impl Builder { /// /// handler.join().unwrap(); /// ``` +/// +/// As mentionned in the module documentation, threads are usualy made to +/// communicate using [`channel`s][`channels`], here is how it usually looks. +/// +/// This example also shows how to use `move`, in order to give ownership +/// of values to a thread. +/// +/// ``` +/// use std::thread; +/// use std::sync::mpsc::channel; +/// +/// let (tx, rx) = channel(); +/// +/// let sender = thread::spawn(move || { +/// tx.send("Hello, thread".to_owned()); +/// }); +/// +/// let receiver = thread::spawn(move || { +/// println!("{}", rx.recv().unwrap()); +/// }); +/// +/// sender.join(); +/// receiver.join(); +/// ``` +/// +/// A thread can also return a value through its [`JoinHandle`], you can use +/// this to make asynchronous computations (futures might be more appropriate +/// though). +/// +/// ``` +/// use std::thread; +/// +/// let computation = thread::spawn(|| { +/// // Some expensive computation. +/// 42 +/// }); +/// +/// let result = computation.join().unwrap(); +/// println!("{}", v); +/// ``` +/// +/// [`channels`]: ../../std/sync/mpsc/index.html +/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html +/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join +/// [`Err`]: ../../std/result/enum.Result.html#variant.Err +/// [`panic`]: ../../std/macro.panic.html +/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn #[stable(feature = "rust1", since = "1.0.0")] pub fn spawn<F, T>(f: F) -> JoinHandle<T> where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static |
