]> git.lizzy.rs Git - rust.git/commitdiff
rollup merge of #20615: aturon/stab-2-thread
authorAlex Crichton <alex@alexcrichton.com>
Tue, 6 Jan 2015 23:38:38 +0000 (15:38 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Tue, 6 Jan 2015 23:38:38 +0000 (15:38 -0800)
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]

28 files changed:
1  2 
src/compiletest/runtest.rs
src/liballoc/arc.rs
src/libcollections/dlist.rs
src/libcore/atomic.rs
src/librustc_driver/lib.rs
src/librustc_trans/back/write.rs
src/librustdoc/lib.rs
src/librustdoc/test.rs
src/libstd/io/comm_adapters.rs
src/libstd/io/mod.rs
src/libstd/io/process.rs
src/libstd/macros.rs
src/libstd/path/posix.rs
src/libstd/path/windows.rs
src/libstd/rand/os.rs
src/libstd/sync/mpsc/mod.rs
src/libstd/sync/mutex.rs
src/libstd/sync/once.rs
src/libstd/sync/rwlock.rs
src/libstd/thread.rs
src/libstd/thread_local/mod.rs
src/libtest/lib.rs
src/test/bench/shootout-fannkuch-redux.rs
src/test/bench/shootout-k-nucleotide-pipes.rs
src/test/bench/shootout-k-nucleotide.rs
src/test/run-pass/logging-only-prints-once.rs
src/test/run-pass/slice-panic-1.rs
src/test/run-pass/slice-panic-2.rs

Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
index 787d0e98e2bfcd79bd97337dc40c90b8f91a3a96,54f74239daf8603c81451cfade6a7ac748d4fc75..a72cfad2cb88ee27682d2506b93f848ddabd1174
@@@ -27,9 -27,9 +27,9 @@@ impl fmt::Show for Foo 
  }
  
  pub fn main() {
-     Thread::spawn(move|| {
+     Thread::scoped(move|| {
          let mut f = Foo(Cell::new(0));
 -        println!("{}", f);
 +        println!("{:?}", f);
          let Foo(ref mut f) = f;
          assert!(f.get() == 1);
      }).join().ok().unwrap();
Simple merge
Simple merge