diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-01-06 15:38:38 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-06 15:38:38 -0800 |
| commit | 36f5d122b80682de473aeda2e20f14b6ceb86d74 (patch) | |
| tree | 84fff634fa8759f4ef8d90651f8a9c242fb8ea51 /src/libstd/sync/mutex.rs | |
| parent | 0631b466c23ffdb1edb2997a8da2702cfe6fcd4a (diff) | |
| parent | caca9b2e7109a148d100a3c6851241d3815da3db (diff) | |
| download | rust-36f5d122b80682de473aeda2e20f14b6ceb86d74.tar.gz rust-36f5d122b80682de473aeda2e20f14b6ceb86d74.zip | |
rollup merge of #20615: aturon/stab-2-thread
This commit takes a first pass at stabilizing `std::thread`: * It removes the `detach` method in favor of two constructors -- `spawn` for detached threads, `scoped` for "scoped" (i.e., must-join) threads. This addresses some of the surprise/frustrating debug sessions with the previous API, in which `spawn` produced a guard that on destruction joined the thread (unless `detach` was called). The reason to have the division in part is that `Send` will soon not imply `'static`, which means that `scoped` thread creation can take a closure over *shared stack data* of the parent thread. On the other hand, this means that the parent must not pop the relevant stack frames while the child thread is running. The `JoinGuard` is used to prevent this from happening by joining on drop (if you have not already explicitly `join`ed.) The APIs around `scoped` are future-proofed for the `Send` changes by taking an additional lifetime parameter. With the current definition of `Send`, this is forced to be `'static`, but when `Send` changes these APIs will gain their full flexibility immediately. Threads that are `spawn`ed, on the other hand, are detached from the start and do not yield an RAII guard. The hope is that, by making `scoped` an explicit opt-in with a very suggestive name, it will be drastically less likely to be caught by a surprising deadlock due to an implicit join at the end of a scope. * The module itself is marked stable. * Existing methods other than `spawn` and `scoped` are marked stable. The migration path is: ```rust Thread::spawn(f).detached() ``` becomes ```rust Thread::spawn(f) ``` while ```rust let res = Thread::spawn(f); res.join() ``` becomes ```rust let res = Thread::scoped(f); res.join() ``` [breaking-change]
Diffstat (limited to 'src/libstd/sync/mutex.rs')
| -rw-r--r-- | src/libstd/sync/mutex.rs | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 9756d086193..c1b55c6ff78 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -75,7 +75,7 @@ use sys_common::mutex as sys; /// tx.send(()).unwrap(); /// } /// // the lock is unlocked here when `data` goes out of scope. -/// }).detach(); +/// }); /// } /// /// rx.recv().unwrap(); @@ -90,7 +90,7 @@ use sys_common::mutex as sys; /// let lock = Arc::new(Mutex::new(0u)); /// let lock2 = lock.clone(); /// -/// let _ = Thread::spawn(move || -> () { +/// let _ = Thread::scoped(move || -> () { /// // This thread will acquire the mutex first, unwrapping the result of /// // `lock` because the lock has not been poisoned. /// let _lock = lock2.lock().unwrap(); @@ -376,9 +376,9 @@ mod test { let (tx, rx) = channel(); for _ in range(0, K) { let tx2 = tx.clone(); - Thread::spawn(move|| { inc(); tx2.send(()).unwrap(); }).detach(); + Thread::spawn(move|| { inc(); tx2.send(()).unwrap(); }); let tx2 = tx.clone(); - Thread::spawn(move|| { inc(); tx2.send(()).unwrap(); }).detach(); + Thread::spawn(move|| { inc(); tx2.send(()).unwrap(); }); } drop(tx); @@ -453,7 +453,7 @@ mod test { fn test_mutex_arc_poison() { let arc = Arc::new(Mutex::new(1i)); let arc2 = arc.clone(); - let _ = Thread::spawn(move|| { + let _ = Thread::scoped(move|| { let lock = arc2.lock().unwrap(); assert_eq!(*lock, 2); }).join(); @@ -480,7 +480,7 @@ mod test { fn test_mutex_arc_access_in_unwind() { let arc = Arc::new(Mutex::new(1i)); let arc2 = arc.clone(); - let _ = Thread::spawn(move|| -> () { + let _ = Thread::scoped(move|| -> () { struct Unwinder { i: Arc<Mutex<int>>, } |
