-
Notifications
You must be signed in to change notification settings - Fork 4.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
Provide a method to retrieve the native handle for a Thread object #37423
Comments
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label. |
What is this API going to return on non-Windows? |
I would assume the |
@kouvel fyi.. |
The problem is that there are typically number of different kinds of thread handles. For example, OSX has
.NET does not work with Windows fibres for fundamendal reasons. This API won't make it possible. |
Isn't |
|
Thanks! For reference: https://man7.org/linux/man-pages/man2/gettid.2.html
|
is it specific to how the win32 fibre implementation works? - or is it fundamentally impossible to use fibres at all in .NET |
Correct. We do not support running managed code on fibers. If you run managed code on fibers, you will get arbitrary crashes. |
Ah, my code already does that so it's fine 😆 |
Being able to do things like set affinity would still be useful, I think. Althought, there could be a managed API to do that specific thing |
I think I've run into changed behavior related to this when trying to port .NET Framework application that was successful at setting ideal processor affinity but that no longer works in .NET 5, the following code no longer matches current thread id: /* --------------------------------------------------------------------------------- */ // note: it's obsolte, but the same value can be had via Thread.CurrentThread.ManagedThreadId - int iCurThreadID=AppDomain.GetCurrentThreadId(); foreach (ProcessThread oPT in Process.GetCurrentProcess().Threads)
} /* --------------------------------------------------------------------------------- */ How are we supported to set processor affinity for current managed thread? |
API-Shape looks good to me. |
Alternative: A property called |
Background and Motivation
For native interop, it can be incredibly useful to have access to the underlying thread handle value. For example
Proposed API
GetNativeHandle
would throw an exception (not sure which one) if theThread
object wasn't a true thread (currently afaik they all are, but in the future they could be fibers etc), whereasTryGetNativeHandle
would return falseUsage Examples
Alternative Designs
You could, in this case, just expose
SetThreadAffinity
- but it doesn't make sense if a thread isn't always a "real" threadRisks
It isn't a breaking change. If misused, I am sure the handle could do some messy things, but that applies to a lot of the framework 😄
The text was updated successfully, but these errors were encountered: