Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How does a thread know what processor it is running on?

6 views
Skip to first unread message

Suggitt@discussions.microsoft.com Gerry Suggitt

unread,
Aug 25, 2005, 2:01:03 PM8/25/05
to
This must be simple but I can't find the answer.

On a multi-processor system, what Windows API calls can I make to obtain the
number of the processor a thread is currently running on?

I want to log information from running applications and it might be useful
to know that two lines in the log file were entered by threads executing
simultaneously. I would add the CPU number to the log entry.

James Antognini [MSFT]

unread,
Aug 25, 2005, 2:08:31 PM8/25/05
to
Even if there is a way to learn what is the current CPU, it can change an
instant later. Rethink your wish.

--
James Antognini
Windows DDK and WDK Support


This posting is provided "AS IS" with no warranties, and confers no rights.

"Gerry Suggitt" <Gerry Sug...@discussions.microsoft.com> wrote in message
news:524A3003-65E5-443B...@microsoft.com...

Skywing

unread,
Aug 25, 2005, 2:11:05 PM8/25/05
to
Well, it could change at any time, so that's really only useful for
performance gathering. If you're on Win2003 or later, you can
GetCurrentProcessorNumber().

Another alternative is to restrict a thread to a specific processor using
SetThreadAffinity -- then you always know it's runinng on a particular
processor.

"Gerry Suggitt" <Gerry Sug...@discussions.microsoft.com> wrote in message
news:524A3003-65E5-443B...@microsoft.com...

Severian [MVP]

unread,
Aug 25, 2005, 2:28:36 PM8/25/05
to

If you're interested in whether threads are running concurrently (and
which thread generated which output), just log the thread ID.

Unless tied to a specific processor, the processor number may change
during your log, during the output, when returning from the output,
etc.

--
Sev

Jochen Kalmbach [MVP]

unread,
Aug 25, 2005, 2:32:01 PM8/25/05
to
Hi Gerry!

> On a multi-processor system, what Windows API calls can I make to obtain the
> number of the processor a thread is currently running on?

A thread can only run on *one* processor at a time...

Take a look at

See also: Multiple Processors
http://msdn.microsoft.com/library/en-us/dllproc/base/multiple_processors.asp

> I want to log information from running applications and it might be useful
> to know that two lines in the log file were entered by threads executing
> simultaneously. I would add the CPU number to the log entry.

For this you should use the ThreadID!

See: GetCurrentThreadId
http://msdn.microsoft.com/library/en-us/dllproc/base/getcurrentthreadid.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Pavel A.

unread,
Aug 25, 2005, 7:59:19 PM8/25/05
to
"Skywing" <skywing_...@valhallalegends.com> wrote in message news:e5hgdBa...@TK2MSFTNGP11.phx.gbl...

> Well, it could change at any time, so that's really only useful for performance gathering. If you're on Win2003 or later, you
> can GetCurrentProcessorNumber().

and if you are not on win2003 - use cpuid instruction.
Processor is identified by it's local apic id and CPU index
(logical CPUs of HT processor have same APIC id).
The cpuid app note on intel site has all relevant details.
The problem is that the code sequience for cpuid is not
atomic, so CPU can switch in the middle...

--PA

Slava M. Usov

unread,
Aug 25, 2005, 8:58:58 PM8/25/05
to
"Pavel A." <pav...@NOwritemeNO.com> wrote in message
news:%23SSOFEd...@TK2MSFTNGP15.phx.gbl...

> "Skywing" <skywing_...@valhallalegends.com> wrote in message
> news:e5hgdBa...@TK2MSFTNGP11.phx.gbl...
>> Well, it could change at any time, so that's really only useful for
>> performance gathering. If you're on Win2003 or later, you
>> can GetCurrentProcessorNumber().
>
> and if you are not on win2003 - use cpuid instruction.
> Processor is identified by it's local apic id and CPU index
> (logical CPUs of HT processor have same APIC id).

But you still can distinguish them via cpuid -- this was discussed here a
while ago.

> The cpuid app note on intel site has all relevant details.

CPU can also be differentiated by their IDTs, which also has been discussed
here.

> The problem is that the code sequience for cpuid is not
> atomic, so CPU can switch in the middle...

Who cares, the result hardly means anything anyway.

S


Gerry Suggitt

unread,
Aug 29, 2005, 11:51:44 AM8/29/05
to
Thanks to everyone. Lots of useful information.


I know The thread can hop in an instant.

The ThreadId will not change when it hops - so logging the threadId does not
help identify the CPU.

But perhaps it is folly, as some have suggested. If I have:
Log("about to do X");
X();
Log("finished X" );

And the log reads:
2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
2005 Aug 27 10:05:03.104 thread=292 cpu=1 finished X

There is no guarantee that X did not execute on CPU 2.

But knowing that X executed on CPU 1 is not really important. I really want
to know that two threads are executing simultaneously. So if I see:
2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
2005 Aug 27 10:05:03.120 thread=306 cpu=2 about to do Y
2005 Aug 27 10:05:03.810 thread=292 cpu=1 finished X
2005 Aug 27 10:05:03.850 thread=306 cpu=2 finished Y

I know that thread 292 and 306 are executing simultaneously.

Of course, if we carry the hopping argument to its extreme, there is no
guarantee that thread 306 executed on CPU 1 but hopped over to CPU 2 just as
the "Log()" function was executed.

But this is not likely especially when the thread has affinity for the CPU
it last executed on.

But in conclusion, perfhaps there is not as much useful information to be
derived from the CPU identification as I originally thought.

Jochen Kalmbach [MVP]

unread,
Aug 29, 2005, 2:34:08 PM8/29/05
to
Hi Gerry!

> I know The thread can hop in an instant.
>
> The ThreadId will not change when it hops - so logging the threadId does not
> help identify the CPU.

Why do you need to identify the CPU?
You only need to identify the thread!


> But perhaps it is folly, as some have suggested. If I have:
> Log("about to do X");
> X();
> Log("finished X" );
>
> And the log reads:
> 2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
> 2005 Aug 27 10:05:03.104 thread=292 cpu=1 finished X
>
> There is no guarantee that X did not execute on CPU 2.

What does this matter?


> But knowing that X executed on CPU 1 is not really important. I really want
> to know that two threads are executing simultaneously. So if I see:
> 2005 Aug 27 10:05:03.103 thread=292 cpu=1 about to do X
> 2005 Aug 27 10:05:03.120 thread=306 cpu=2 about to do Y
> 2005 Aug 27 10:05:03.810 thread=292 cpu=1 finished X
> 2005 Aug 27 10:05:03.850 thread=306 cpu=2 finished Y
>
> I know that thread 292 and 306 are executing simultaneously.
>
> Of course, if we carry the hopping argument to its extreme, there is no
> guarantee that thread 306 executed on CPU 1 but hopped over to CPU 2 just as
> the "Log()" function was executed.
>
> But this is not likely especially when the thread has affinity for the CPU
> it last executed on.
>
> But in conclusion, perfhaps there is not as much useful information to be
> derived from the CPU identification as I originally thought.

Yes.

0 new messages