Skip to content

Commit 42ce11e

Browse files
authored
Rollup merge of #41854 - gamazeps:thread-spawn-doc, r=steveklabnik
[Doc] Improve `thread::spawn` documentation Part of #29378 - Add two examples to `thread::spawn` doumentation that show common uses of threads. - Add a link to `thread::Builder` in the `thread::spawn` documentation for configuring threads. - Add a link to `thread::spawn` in `thread::Builder` in order to avoid documentation duplication. r? @steveklabnik
2 parents ec85859 + fe7b6db commit 42ce11e

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

src/libstd/thread/mod.rs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ impl Builder {
287287
/// thread finishes). The join handle can be used to block on
288288
/// termination of the child thread, including recovering its panics.
289289
///
290+
/// For a more complete documentation see [`thread::spawn`][`spawn`].
291+
///
290292
/// # Errors
291293
///
292294
/// Unlike the [`spawn`] free function, this method yields an
@@ -361,19 +363,19 @@ impl Builder {
361363
/// panics, [`join`] will return an [`Err`] containing the argument given to
362364
/// [`panic`].
363365
///
366+
/// This will create a thread using default parameters of [`Builder`], if you
367+
/// want to specify the stack size or the name of the thread, use this API
368+
/// instead.
369+
///
364370
/// # Panics
365371
///
366372
/// Panics if the OS fails to create a thread; use [`Builder::spawn`]
367373
/// to recover from such errors.
368374
///
369-
/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
370-
/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
371-
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
372-
/// [`panic`]: ../../std/macro.panic.html
373-
/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
374-
///
375375
/// # Examples
376376
///
377+
/// Creating a thread.
378+
///
377379
/// ```
378380
/// use std::thread;
379381
///
@@ -383,6 +385,54 @@ impl Builder {
383385
///
384386
/// handler.join().unwrap();
385387
/// ```
388+
///
389+
/// As mentioned in the module documentation, threads are usually made to
390+
/// communicate using [`channels`], here is how it usually looks.
391+
///
392+
/// This example also shows how to use `move`, in order to give ownership
393+
/// of values to a thread.
394+
///
395+
/// ```
396+
/// use std::thread;
397+
/// use std::sync::mpsc::channel;
398+
///
399+
/// let (tx, rx) = channel();
400+
///
401+
/// let sender = thread::spawn(move || {
402+
/// let _ = tx.send("Hello, thread".to_owned());
403+
/// });
404+
///
405+
/// let receiver = thread::spawn(move || {
406+
/// println!("{}", rx.recv().unwrap());
407+
/// });
408+
///
409+
/// let _ = sender.join();
410+
/// let _ = receiver.join();
411+
/// ```
412+
///
413+
/// A thread can also return a value through its [`JoinHandle`], you can use
414+
/// this to make asynchronous computations (futures might be more appropriate
415+
/// though).
416+
///
417+
/// ```
418+
/// use std::thread;
419+
///
420+
/// let computation = thread::spawn(|| {
421+
/// // Some expensive computation.
422+
/// 42
423+
/// });
424+
///
425+
/// let result = computation.join().unwrap();
426+
/// println!("{}", result);
427+
/// ```
428+
///
429+
/// [`channels`]: ../../std/sync/mpsc/index.html
430+
/// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
431+
/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join
432+
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
433+
/// [`panic`]: ../../std/macro.panic.html
434+
/// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
435+
/// [`Builder`]: ../../std/thread/struct.Builder.html
386436
#[stable(feature = "rust1", since = "1.0.0")]
387437
pub fn spawn<F, T>(f: F) -> JoinHandle<T> where
388438
F: FnOnce() -> T, F: Send + 'static, T: Send + 'static

0 commit comments

Comments
 (0)