@@ -287,6 +287,8 @@ impl Builder {
287
287
/// thread finishes). The join handle can be used to block on
288
288
/// termination of the child thread, including recovering its panics.
289
289
///
290
+ /// For a more complete documentation see [`thread::spawn`][`spawn`].
291
+ ///
290
292
/// # Errors
291
293
///
292
294
/// Unlike the [`spawn`] free function, this method yields an
@@ -361,19 +363,19 @@ impl Builder {
361
363
/// panics, [`join`] will return an [`Err`] containing the argument given to
362
364
/// [`panic`].
363
365
///
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
+ ///
364
370
/// # Panics
365
371
///
366
372
/// Panics if the OS fails to create a thread; use [`Builder::spawn`]
367
373
/// to recover from such errors.
368
374
///
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
- ///
375
375
/// # Examples
376
376
///
377
+ /// Creating a thread.
378
+ ///
377
379
/// ```
378
380
/// use std::thread;
379
381
///
@@ -383,6 +385,54 @@ impl Builder {
383
385
///
384
386
/// handler.join().unwrap();
385
387
/// ```
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
386
436
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
387
437
pub fn spawn < F , T > ( f : F ) -> JoinHandle < T > where
388
438
F : FnOnce ( ) -> T , F : Send + ' static , T : Send + ' static
0 commit comments