rust-lang / rust-lang/rust

Document memory ordering guarantees for `std::thread::spawn()`

Open
#119,519 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-atomic A-docs A-thread C-feature-request T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked documentation for std:🧵:spawn(), Builder::spawn, and JoinHandle::join(). Compare the existing memory-ordering language and document the corresponding guarantee for thread creation. Done means the spawn APIs explicitly state the ordering guarantee consistently with join().

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.