Skip to content
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

Make it possible to compare threads #1435

Closed
tomaka opened this issue Dec 29, 2015 · 5 comments
Closed

Make it possible to compare threads #1435

tomaka opened this issue Dec 29, 2015 · 5 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@tomaka
Copy link

tomaka commented Dec 29, 2015

Objects of type Thread can't be compared. There's no way to know if you are still in the same thread as you were before.

I don't know if this should just be an implementation of Eq on Thread, or if a new ThreadId type would need to be created.

@sfackler
Copy link
Member

Should be pretty simple - pthread_equals on not-Windows and GetThreadId on Windows.

@nagisa
Copy link
Member

nagisa commented Dec 29, 2015

There has been a bunch of attempts to implement this already:
rust-lang/rust#29457
rust-lang/rust#29448
rust-lang/rust#29447

Also see:
rust-lang/rust#21507

@nrc nrc added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Aug 19, 2016
@ranma42
Copy link
Contributor

ranma42 commented Sep 28, 2016

@alexcrichton your comment in one of the implementation attempts suggests that [Partial]Eq should be implemented on a ThreadId type and not on Thread itself.
What are the advantages of that approach? What other operations (besides Eq) would you expect to be implemented for ThreadId?

@alexcrichton
Copy link
Member

Threads themselves are somewhat more opaque and it's in general just a little more flexible to deal with ids instead of threads themselves. We could also lower the ThreadId to the underlying primitive if that's the implementation detail.

bors added a commit to rust-lang/rust that referenced this issue Oct 10, 2016
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
@tomaka
Copy link
Author

tomaka commented Jul 22, 2017

ThreadId now exists.

@tomaka tomaka closed this as completed Jul 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

6 participants