Skip to content

Document memory ordering guarantees for std::thread::spawn() #119519

Open
@lukaslueg

Description

@lukaslueg

The documentation for std::thread::JoinHandle::join() guarantees that all memory operations performed by the joined thread are visible in the joining thread before join() returns:

[...] In other words, all operations performed by that thread happen before all operations that happen after join returns.

The documentation does not give such guarantees for std::thread::spawn() and the associated [Builder::spawn].

Obviously, such guarantee already exists implicitly:

fn main() {
    let s = "Foo".to_string();
    std::thread::spawn(move || {
        // All memory-operations performed by the spawning thread
        // are visible (happened-before) when the closure starts
        // executing, even though they have no explicit ordering.
        println!("{s}");
    }).join().unwrap();
}

Can we update the documentation on std::thread::spawn() (and friends) to explicitly make this guarantee?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-atomicArea: Atomics, barriers, and sync primitivesA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-threadArea: `std::thread`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions