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]
This commit is contained in:
Alex Crichton 2015-01-06 15:38:38 -08:00
commit 36f5d122b8
97 changed files with 361 additions and 293 deletions

View file

@ -395,7 +395,7 @@ fn main() {
for _ in range(0u, 10u) {
Thread::spawn(move || {
println!("Hello, world!");
}).detach();
});
}
}
```
@ -405,8 +405,7 @@ This program creates ten threads, who all print `Hello, world!`. The
double bars `||`. (The `move` keyword indicates that the closure takes
ownership of any data it uses; we'll have more on the significance of
this shortly.) This closure is executed in a new thread created by
`spawn`. The `detach` method means that the child thread is allowed to
outlive its parent.
`spawn`.
One common form of problem in concurrent programs is a 'data race.'
This occurs when two different threads attempt to access the same
@ -429,7 +428,7 @@ fn main() {
for i in range(0u, 3u) {
Thread::spawn(move || {
for j in range(0, 3) { numbers[j] += 1 }
}).detach();
});
}
}
```
@ -488,7 +487,7 @@ fn main() {
(*array)[i] += 1;
println!("numbers[{}] is {}", i, (*array)[i]);
}).detach();
});
}
}
```