Skip to content

Tracking Issue for Thread::{into_raw, from_raw} #97523

Open

Description

@ibraheemdev

Feature gate: #![feature(thread_raw)]

This is a tracking issue for Thread::into_raw and Thread::from_raw.

These functions allow conversions between a Thread and a raw pointer, which is useful in concurrent code as it allows storing a Thread in an AtomicPtr.

Public API

// std::thread

impl Thread {
    fn into_raw(self) -> *const ();
    unsafe fn from_raw(ptr: *const ()) -> Thread;    
}

Steps / History

Unresolved Questions

  • Do we want to commit to guaranteeing that Thread can always be represented as a pointer/integer?

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on May 29, 2022
  2. yanchith commented on Feb 7, 2025

    @yanchith
    Contributor

    Not sure If I am missing something, but into_raw requires the thread by value, and that is not always available. E.g. JoinHandle::thread only gives out a shared reference.

    How can I unpark a thread for which I just have a JoinHandle?

  3. ibraheemdev commented on Feb 7, 2025

    @ibraheemdev
    MemberAuthor

    @yanchith Thread is cloneable, but it might make sense to have a as_raw method as well.

  4. yanchith commented on Feb 9, 2025

    @yanchith
    Contributor

    Yeah, thanks! I didn't notice Thread was Clone. I am perfectly happy with that now.

    Thread::as_raw: sure, if it can be designed such that the data inside the Arc inside Thread doesn't get dropped meanwhile. Thread::into_inner leaks the Arc, so that's taken care of.

  5. zesterer commented on May 25, 2025

    @zesterer
    Contributor

    As a point of interest: is it legal to use Thread::from_raw if the pointer is shared in a multi-threaded context? A lot of code assumes this to be true for Arc::from_raw, and of course the implementation uses an Arc, but it is non-obvious.

    Use of Thread::from_raw in a multi-threaded context might result in two Threads existing that both point to the same block of memory, but with the strong count still being 1 (even if momentarily).

    An API like fn Thread::from_raw_ref(ptr: *const ()) -> &Thread would make it much easier to use this API correctly (and avoid a lot of error-prone core::mem::forget in referential contexts to boot). With Arc, this is already trivially possible since the pointer you get back is a *const T, but here we only get a *const () so you can't even temporarily turn it into a reference to the original Thread.

  6. orlp commented on Dec 20, 2025

    @orlp
    Contributor

    @zesterer It's fairly obvious to me that it's unsound to call Thread::from_raw twice in an unsynchronized manner if one of them can drop the value in the meantime. I personally don't think the from_raw_ref API is necessary, and it would still be dangerous if someone calls from_raw_ref at the same time as someone else calls from_raw.

    Regarding the unresolved question,

    Do we want to commit to guaranteeing that Thread can always be represented as a pointer/integer?

    This API inherently guarantees that the Thread can be represented as a pointer.

    I fail to see any reason why we would guarantee that pointer could be converted to/from an integer outside of the usual expose_provenance / with_exposed_provenance rules which are already valid today. If that would really be desired it could also be added later.


    I personally don't see anything that's blocking this for a FCP / stabilization. Could we move along with that?

  7. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
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

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-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