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