Skip to content

JoinHandle::join doesn't document interaction with atomics #45467

@hniksic

Description

@hniksic
Contributor

The current documentation of JoinHandle::join doesn't specify the effect of joining the thread on the atomics that the thread modified. I couldn't find any other place in the documentation where this would be described either.

Without additional guarantees, the call to AtomicUsize::into_inner in the following snippet could be understood to constitute a data race:

let i_ref = Arc::new(AtomicUsize::new(0));
let thr = std::thread::spawn({
    let i_ref = i_ref.clone();
    move || i_ref.store(1, Ordering:: Relaxed)
});
thr.join().unwrap();
let i = Arc::try_unwrap(i_ref).unwrap().into_inner();
assert_eq!(i, 1);

In comparison, cppreference explicitly states that "The completion of the thread identified by *this synchronizes with the corresponding successful return from join()."

The nomicon does claim that Rust just inherits C11's memory model for atomics, which could be used to infer that JoinHandle::join has the same effect as C++'s std::thread::join. It would still be a good idea to document the synchronizes-with relation explicitly to avoid confusion such as in this stackoverflow question.

Activity

added
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
on Oct 23, 2017
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Oct 24, 2017
steveklabnik

steveklabnik commented on Oct 31, 2017

@steveklabnik
Member

@rust-lang/libs @rust-lang/lang , can we get a clarification on intended semantics here?

added
T-langRelevant to the language team
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Oct 31, 2017
alexcrichton

alexcrichton commented on Oct 31, 2017

@alexcrichton
Member

Discussed during libs triage today, the conclusion was to just document what C++ does as we're built on the same primitives

added and removed
T-langRelevant to the language team
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Nov 21, 2017
RalfJung

RalfJung commented on Aug 15, 2018

@RalfJung
Member

Should be fixed by #53389... @hniksic could you have a look?

hniksic

hniksic commented on Aug 17, 2018

@hniksic
ContributorAuthor

@RalfJung Looks great, thanks!

added a commit that references this issue on Aug 28, 2018
added a commit that references this issue on Aug 28, 2018
added 3 commits that reference this issue on Aug 29, 2018
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-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@alexcrichton@kennytm@RalfJung@hniksic

        Issue actions

          JoinHandle::join doesn't document interaction with atomics · Issue #45467 · rust-lang/rust