@@ -390,7 +390,66 @@ impl Builder {
390
390
unsafe { self . spawn_unchecked ( f) }
391
391
}
392
392
393
- /// FIXME: Doc
393
+ /// Spawns a new thread without any lifetime restrictions by taking ownership
394
+ /// of the `Builder`, and returns an [`io::Result`] to its [`JoinHandle`].
395
+ ///
396
+ /// The spawned thread may outlive the caller (unless the caller thread
397
+ /// is the main thread; the whole process is terminated when the main
398
+ /// thread finishes). The join handle can be used to block on
399
+ /// termination of the child thread, including recovering its panics.
400
+ ///
401
+ /// This method is identical to [`thread::Builder::spawn`][`Builder::spawn`],
402
+ /// except for the relaxed lifetime bounds, which render it unsafe.
403
+ /// For a more complete documentation see [`thread::spawn`][`spawn`].
404
+ ///
405
+ /// # Errors
406
+ ///
407
+ /// Unlike the [`spawn`] free function, this method yields an
408
+ /// [`io::Result`] to capture any failure to create the thread at
409
+ /// the OS level.
410
+ ///
411
+ /// # Panics
412
+ ///
413
+ /// Panics if a thread name was set and it contained null bytes.
414
+ ///
415
+ /// # Safety
416
+ ///
417
+ /// The caller has to ensure that no references in the supplied thread closure
418
+ /// or its return type can outlive the spawned thread's lifetime. This can be
419
+ /// guaranteed in two ways:
420
+ ///
421
+ /// - ensure that [`join`][`JoinHandle::join`] is called before any referenced
422
+ /// data is dropped
423
+ /// - use only types with `'static` lifetime bounds, i.e. those with no or only
424
+ /// `'static` references (both [`thread::Builder::spawn`][`Builder::spawn`]
425
+ /// and [`thread::spawn`][`spawn`] enforce this property statically)
426
+ ///
427
+ /// # Examples
428
+ ///
429
+ /// ```
430
+ /// use std::thread;
431
+ ///
432
+ /// let builder = thread::Builder::new();
433
+ ///
434
+ /// let x = 1;
435
+ /// let thread_x = &x;
436
+ ///
437
+ /// let handler = unsafe {
438
+ /// builder.spawn_unchecked(move || {
439
+ /// println!("x = {}", *thread_x);
440
+ /// }).unwrap();
441
+ /// }
442
+ ///
443
+ /// // caller has to ensure `join()` is called, otherwise
444
+ /// // it is possible to access freed memory if `x` gets
445
+ /// // dropped before the thread closure is executed!
446
+ /// handler.join.unwrap();
447
+ /// ```
448
+ ///
449
+ /// [`spawn`]: ../../std/thread/fn.spawn.html
450
+ /// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
451
+ /// [`io::Result`]: ../../std/io/type.Result.html
452
+ /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
394
453
#[ unstable( feature = "thread_spawn_unchecked" , issue = "0" ) ]
395
454
pub unsafe fn spawn_unchecked < F , T > ( self , f : F ) -> io:: Result < JoinHandle < T > > where
396
455
F : FnOnce ( ) -> T , F : Send , T : Send
0 commit comments