Physical vs logical threads question RRS feed

  • Question

  • Hello,

    I understand that starting from .NET 2.0 there is no 1-1 correlation between physical and logical threads, that it is usuall that more than one logical thread is running on 1 phyisical thread.

    What I'm interested in is can we get any information via managed environment on information about logical threads residing on which physical threads.

    While I realize that it is impossible to see for example how much memory each logical thread is consuming, but knowing which logical threads operate on which physical one we can obtain some sort of information via Performance Counters.

    Is my reasoning correct on this?

    Basically I'd like to be able to monitor threads in my applications a little better.

    So, is it possible to obtain any information about which logical threads belong to which physical ones and then use this information to feed this to performance counters?

    Thanks a lot in advance for any information.

    Marcin Waligora
    Thursday, October 9, 2008 7:50 PM


  • It depends on the CLR host you are using.  The default host, the one you get when you run a .exe on Windows has a one-to-one mapping between Thread instances and Windows threads.  SQL Server 2005 is a host that is rumored to not use physical Windows threads but fibers.  Not so sure if that's actually true, it was planned though.

    Threads don't own memory, other than the one megabyte chunk reserved for their stack.  All other memory they may touch is shared between all other threads in the process.  Correspondingly, there are no Performance Counters that let you measure anything like "how much memory each logical thread is consuming".

    The ProcessThread class is the class that encapsules physical threads in the process, get it from Process.Threads property.  It is not possible to map a ProcessThread to a Thread instance unless you can P/Invoke the GetCurrentThreadId() API function in the Thread's code so you can compare it to ProcessThread.Id.  This is not practical for any other thread than the one that is updating your thread statistics.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Thursday, October 16, 2008 5:34 AM
    Thursday, October 9, 2008 9:31 PM