From 986549e9f565091ec4bb70cf456029a572da8046 Mon Sep 17 00:00:00 2001 From: oliver-giersch Date: Mon, 15 Oct 2018 12:48:24 +0200 Subject: adds doc for `Builder::spawn_unchecked` --- src/libstd/thread/mod.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'src/libstd/thread') diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 2f63963c823..73251d40599 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -390,7 +390,66 @@ impl Builder { unsafe { self.spawn_unchecked(f) } } - /// FIXME: Doc + /// Spawns a new thread without any lifetime restrictions by taking ownership + /// of the `Builder`, and returns an [`io::Result`] to its [`JoinHandle`]. + /// + /// The spawned thread may outlive the caller (unless the caller thread + /// is the main thread; the whole process is terminated when the main + /// thread finishes). The join handle can be used to block on + /// termination of the child thread, including recovering its panics. + /// + /// This method is identical to [`thread::Builder::spawn`][`Builder::spawn`], + /// except for the relaxed lifetime bounds, which render it unsafe. + /// For a more complete documentation see [`thread::spawn`][`spawn`]. + /// + /// # Errors + /// + /// Unlike the [`spawn`] free function, this method yields an + /// [`io::Result`] to capture any failure to create the thread at + /// the OS level. + /// + /// # Panics + /// + /// Panics if a thread name was set and it contained null bytes. + /// + /// # Safety + /// + /// The caller has to ensure that no references in the supplied thread closure + /// or its return type can outlive the spawned thread's lifetime. This can be + /// guaranteed in two ways: + /// + /// - ensure that [`join`][`JoinHandle::join`] is called before any referenced + /// data is dropped + /// - use only types with `'static` lifetime bounds, i.e. those with no or only + /// `'static` references (both [`thread::Builder::spawn`][`Builder::spawn`] + /// and [`thread::spawn`][`spawn`] enforce this property statically) + /// + /// # Examples + /// + /// ``` + /// use std::thread; + /// + /// let builder = thread::Builder::new(); + /// + /// let x = 1; + /// let thread_x = &x; + /// + /// let handler = unsafe { + /// builder.spawn_unchecked(move || { + /// println!("x = {}", *thread_x); + /// }).unwrap(); + /// } + /// + /// // caller has to ensure `join()` is called, otherwise + /// // it is possible to access freed memory if `x` gets + /// // dropped before the thread closure is executed! + /// handler.join.unwrap(); + /// ``` + /// + /// [`spawn`]: ../../std/thread/fn.spawn.html + /// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn + /// [`io::Result`]: ../../std/io/type.Result.html + /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html #[unstable(feature = "thread_spawn_unchecked", issue = "0")] pub unsafe fn spawn_unchecked(self, f: F) -> io::Result> where F: FnOnce() -> T, F: Send, T: Send -- cgit 1.4.1-3-g733a5 From 9d7a83862b77e36aa165bf843a03f85adcd271bf Mon Sep 17 00:00:00 2001 From: oliver-giersch Date: Mon, 15 Oct 2018 13:22:39 +0200 Subject: fixes misplaced semicolon --- src/libstd/thread/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libstd/thread') diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 73251d40599..afc659ccf3a 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -437,8 +437,8 @@ impl Builder { /// let handler = unsafe { /// builder.spawn_unchecked(move || { /// println!("x = {}", *thread_x); - /// }).unwrap(); - /// } + /// }).unwrap() + /// }; /// /// // caller has to ensure `join()` is called, otherwise /// // it is possible to access freed memory if `x` gets -- cgit 1.4.1-3-g733a5 From ee5703cbbc6579fde6c717a6b27d58588294d6e1 Mon Sep 17 00:00:00 2001 From: oliver-giersch Date: Mon, 15 Oct 2018 13:47:27 +0200 Subject: adds missing method call parentheses --- src/libstd/thread/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd/thread') diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index afc659ccf3a..770044439a5 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -443,7 +443,7 @@ impl Builder { /// // caller has to ensure `join()` is called, otherwise /// // it is possible to access freed memory if `x` gets /// // dropped before the thread closure is executed! - /// handler.join.unwrap(); + /// handler.join().unwrap(); /// ``` /// /// [`spawn`]: ../../std/thread/fn.spawn.html -- cgit 1.4.1-3-g733a5 From ebb9d289db4f547798a1700175fd7de979051c39 Mon Sep 17 00:00:00 2001 From: oliver-giersch Date: Mon, 15 Oct 2018 14:14:17 +0200 Subject: adds feature gate to doc-test (example) --- src/libstd/thread/mod.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libstd/thread') diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 770044439a5..8df9bee0eb4 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -427,6 +427,7 @@ impl Builder { /// # Examples /// /// ``` + /// #![feature(thread_spawn_unchecked)] /// use std::thread; /// /// let builder = thread::Builder::new(); -- cgit 1.4.1-3-g733a5