Next time I run into .NET and native threading issues:
Main UI threads often have a correlation between managed and native thread IDs.
But for other threads, you cannot be really sure.
Some background articles on this:
- Threading deep dive – Day 9 – My Adventures with Microsoft Technologies – Site Home – MSDN Blogs: what happens when you have short-lived managed threads.
- Threads, fibers, stacks and address space – cbrumme’s WebLog – Site Home – MSDN Blogs: multiple managed threads might map to the same native thread.
- Win32 native threads and .Net AppDomains – Ars Technica OpenForum.
- c# – How do I get the _real_ thread id in a CLR “friendly” way? – Stack Overflow: It’s perfectly legal from a CLR perspective for a single managed thread to be backed by several different native threads during it’s lifetime.
- Native vs Managed Threads.
- c# – Getting the thread ID from a thread – Stack Overflow: You don’t synchronize on ID’s though, you use synchronization primitives like mutexes. This is only for debugging purposes.
- c# – Relationship between ManagedThreadID and Operating System ThreadID – Stack Overflow.
- .net – AppDomain.GetCurrentThreadID vs Thread.ManagedThreadID for Windows API calls? – Stack Overflow: There are also System.Threading.Thread.BeginThreadAffinity() / EndThreadAffinity() functions that purportedly stop the VM from switching between different physical threads.
–jeroen