For native interop, it can be incredibly useful to have access to the underlying thread handle value. For example
public class Thread
{
+ public IntPtr GetNativeHandle();
+ public bool TryGetNativeHandle(out IntPtr handle);
}
GetNativeHandle would throw an exception (not sure which one) if the Thread object wasn't a true thread (currently afaik they all are, but in the future they could be fibers etc), whereas TryGetNativeHandle would return false
var thread = ...;
// lock the thread to the first logical processor
Win32.SetThreadAffinityMask(thread.GetNativeHandle(), 1);
You could, in this case, just expose SetThreadAffinity - but it doesn't make sense if a thread isn't always a "real" thread
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 馃槃
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
cc @tannergooding
What is this API going to return on non-Windows?
I would assume the pthread_t* on linux systems, and on other systems the type the runtime uses to create them with the OS
@kouvel fyi..
The problem is that there are typically number of different kinds of thread handles. For example, OSX has pthread_t*, mach_port_t and TID. Even pthread_t* on Linux is not the ubiquitous thread ID. For example, the Linux OS syscalls take pid_t, not pthread_t*.
Working with native fibres
Creating custom fibre libraries
.NET does not work with Windows fibres for fundamendal reasons. This API won't make it possible.
Isn't pid_t the process ID and not the thread handle?
pid_t is used for both process id and thread id (gettid() returns it for current thread, for example)
Thanks! For reference: https://man7.org/linux/man-pages/man2/gettid.2.html
In a single-threaded process, the thread ID is equal to the process ID (PID, as returned by getpid(2)). In a multithreaded process, all threads have the same PID, but each one has a unique TID.
...
The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).
.NET does not work with Windows fibres for fundamendal reasons. This API won't make it possible.
is it specific to how the win32 fibre implementation works? - or is it fundamentally impossible to use fibres at all in .NET
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.
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
Most helpful comment
Ah, my code already does that so it's fine 馃槅