Skip to content

proposal for new task API #10614

Closed
Closed
@thestinger

Description

@thestinger
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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions