T.E.D.
T.E.D.

Reputation: 44804

How can I find which processor each thread is running on?

I have a multi-threaded program, and have been asked to add in an option to put every thread on its very own core (CPU, processor, whatever).

I've written code to do this using SetThreadAffinityMask(). I get the process' default affinity mask, verify that there are enough processors to do it, then set each thread's affinity mask to include only one of the available processors (and different for each thread).

All this seems to compile and run with no problem, but how do I check if it is actually working?

It seems like there should be some monitoring tool that shows what CPU a thread is running on, but I can't find it. ProcessExplorer will show a thread's CPU utilization, but not which CPU that occurred on. I looked through the possible counters that can be used on perfmon, but again I could only find % utilization, not which CPU.

It's been suggested I do a GetThreadAffinityMask() and report that, but it seems to me that will just report that I called SetThreadAffinityMask() correctly.

So am I just supposed to take it on faith that SetThreadAffinity() will be doing exactly what I want?

(NOTE: I found quite a few seemingly similar questions on SO, but none that actually contain an answer for this particular question).

Upvotes: 3

Views: 2312

Answers (2)

Reed Copsey
Reed Copsey

Reputation: 564413

You can call GetCurrentProcessorNumber to get the processor number of the currently executing thread.

Upvotes: 1

JosephH
JosephH

Reputation: 8815

Use GetCurrentProcessorNumber() ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms683181(v=vs.85).aspx )

Upvotes: 3

Related Questions