Skip to content

Document thread_local behavior with foreign-created threads #136546

Open
@eloff

Description

@eloff

Location

https://doc.rust-lang.org/std/macro.thread_local.html
https://doc.rust-lang.org/std/thread/struct.LocalKey.html

Summary

Rust can be used as a static or dynamic library in a host process in another language/runtime. The thread_local documentation makes no mention about how foreign threads behave with respect to to thread locals created via LocalKey or the !thread_local macro.

I think it is safe, with one possible exception. I think the const {} syntax allows Rust to avoid lazy initialization and instead use a more efficient implementation that may rely on statically allocated thread-local storage (TLS) instead of dynamically allocated TLS keys. I do not see how this would work with foreign-created threads that call into Rust.

Researching this has proved difficult, I think the docs should clarify the behavior and what is guaranteed and what isn't when it comes to foreign-created threads.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-thread-localsArea: Thread local storage (TLS)T-libsRelevant to the library 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