@@ -390,7 +390,67 @@ 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
+ /// #![feature(thread_spawn_unchecked)]
431
+ /// use std::thread;
432
+ ///
433
+ /// let builder = thread::Builder::new();
434
+ ///
435
+ /// let x = 1;
436
+ /// let thread_x = &x;
437
+ ///
438
+ /// let handler = unsafe {
439
+ /// builder.spawn_unchecked(move || {
440
+ /// println!("x = {}", *thread_x);
441
+ /// }).unwrap()
442
+ /// };
443
+ ///
444
+ /// // caller has to ensure `join()` is called, otherwise
445
+ /// // it is possible to access freed memory if `x` gets
446
+ /// // dropped before the thread closure is executed!
447
+ /// handler.join().unwrap();
448
+ /// ```
449
+ ///
450
+ /// [`spawn`]: ../../std/thread/fn.spawn.html
451
+ /// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn
452
+ /// [`io::Result`]: ../../std/io/type.Result.html
453
+ /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
394
454
#[ unstable( feature = "thread_spawn_unchecked" , issue = "0" ) ]
395
455
pub unsafe fn spawn_unchecked < F , T > ( self , f : F ) -> io:: Result < JoinHandle < T > > where
396
456
F : FnOnce ( ) -> T , F : Send , T : Send
0 commit comments