-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking issue for std::thread::ThreadId #21507
Comments
I'd also like to have |
I'd very much like some kind of thread id, I'm pretty sure every Thread implementation around gives numeric ids (Python has a string |
FYI, here's the associated issue in the RFCs repo: rust-lang/rfcs#1435 |
Add ThreadId for comparing threads This adds the capability to store and compare threads with the current calling thread via a new struct, `std::thread::ThreadId`. Addresses the need outlined in issue #21507. This avoids the need to add any special checks to the existing thread structs and does not rely on the system to provide an identifier for a thread, since it seems that this approach is unreliable and undesirable. Instead, this simply uses a lazily-created, thread-local `usize` whose value is copied from a global atomic counter. The code should be simple enough that it should be as much reliable as the `#[thread_local]` attribute it uses (however much that is). `ThreadId`s can be compared directly for equality and have copy semantics. Also see these other attempts: - #29457 - #29448 - #29447 And this in the RFC repo: rust-lang/rfcs#1435
I think it would be fitting to implement |
Just to chime in here, it would be really nice to have a proper |
There's a PR open to implement |
Derive Hash for ThreadId + better example Derive `Hash` for `ThreadId` (see comments in rust-lang#21507). Useful for making maps based on thread, e.g. `HashMap<ThreadId, ?>`. Also update example code for thread IDs to be more useful.
Derive Hash for ThreadId + better example Derive `Hash` for `ThreadId` (see comments in rust-lang#21507). Useful for making maps based on thread, e.g. `HashMap<ThreadId, ?>`. Also update example code for thread IDs to be more useful.
Derive Hash for ThreadId + better example Derive `Hash` for `ThreadId` (see comments in #21507). Useful for making maps based on thread, e.g. `HashMap<ThreadId, ?>`. Also update example code for thread IDs to be more useful.
@rust-lang/libs, thoughts on stabilization? |
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
BTreeMap wants to know whether we could get an impl Ord for ThreadId. Are there any reasons not to have one? |
|
I'd personally go either way on |
I'm not super concerned about the Ord impl. We certainly don't want to promise that IDs sort in order of creation - TIDs on Unix systems wrap around pretty frequently. |
FWIW, in the current implementation it's a monotonic u64 counter, with no relationship to the OS TID at all. So it will in fact sort in the order of creation, or the order when first seen for threads created outside of Rust. But I still wouldn't want to promise this. |
If |
@dlight Currently, it already implements I don't see an issue with implementing The only issue I see is if we change the implementation later down the road to use system handles, it would be difficult to provide a stable |
We wouldn't be able to implement |
System handles also aren't globally unique when threads stop and new ones start. Is that a property we want to guarantee about |
That's a good point. I think that's why we chose not to use system handles in the first place: it makes @cuviper We don't currently make such a guarantee (which should reflect in the docs), but I think we should guarantee globally unique IDs. Assuming we don't plan on changing the IDs from being a custom integer. If the range of IDs becomes a problem, it is easy enough to increase the number of bits being used and maintain that guarantee. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
Update description
Tracking issue for
std::thread::ThreadId
. Points to consider when stabilizing:ThreadId
, includingCopy
Original report
Not sure if it can be considered as bug but it sure cripples the existing threading API (e.g., makes it harder for a thread to identify itself or store/pass its identity).
// it could also be nice if Thread was able to return a unique integer id() (or hash itself?..).
The text was updated successfully, but these errors were encountered: