Skip to content

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

@lukaslueg

Description

@lukaslueg
Contributor

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?

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jan 2, 2024
Noratrieb

Noratrieb commented on Jan 3, 2024

@Noratrieb
Member

@rustbot label -needs-triage +A-docs +T-libs-api +C-feature-request +A-thread +A-atomic

added
A-atomicArea: Atomics, barriers, and sync primitives
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
A-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.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @lukaslueg@rustbot@Noratrieb

        Issue actions

          Document memory ordering guarantees for `std::thread::spawn()` · Issue #119519 · rust-lang/rust