I'm using pdh to generate Performace data for a thread in my multithreaded program.
The counter should be like
"\\Thread(App/3)\\% Processor Time"
here App/3 is the name of the instance for the thread.
My question is:
I know the thread Id for my thread, how do I find the instance name of the thread from my program?
Note that the name you see in the performance counter is not a thread name. It is just a friendly name that performance monitor uses for display. What is more important is the thread ID.
Given a thread, you can find the thread ID by adding a counter.
1. Select the performance object as thread. Now, you see a list of the processes and their threads in the list.
2. In the Select Counters from list, select ID Thread.
3. In the select instances from list, select the thread you are interested in.
In your code add the GetCurrentThreadID in each thread and trace out the thread ID. Compare the 2.
If you need the order of thread creation use Thread32First(...) in the tool help library. Or use the Native Api via NtQuerySystemInformation(...). Or WMI. The performance monitor should have a counter to enum the threads also.