Closed
Description
fn spawn(proc() -> A) -> Task<A> { ... }
fn spawn_detached(proc()) { ... }
let a = spawn(proc() { ... });
let b = spawn(proc() { ... });
let sum = *a.join() + *b.join();
The Task
struct will have a destructor calling join, so there will always be a synchronization point with spawn
. If the task fails, the join
call will propagate the failure. A try_join
method can be added to handle an error.
let mut tasks = ~[];
for i in range(0, n) {
tasks.push(spawn(proc() { ... });
}
for task in tasks.move_iter() {
match task.try_join() {
Err(x) => handle(x), _ => ()
}
}
A more complex example:
https://github.com/thestinger/rust-core/blob/master/test/thread.rs#L41-L78
Note that since it's using detached threads, it will happily leak the proc
and the queue in cases of pathological scheduling. Of course, that's just a case of exiting early without doing more work than required.
Metadata
Metadata
Assignees
Labels
No labels