Hi Willy,
Willy Denoyette said:
TT (Tom Tempelaere) said:
Willy,
"Willy Denoyette [MVP]" <
[email protected]> schreef in bericht
[...]
How to do that . is there any whay to do something like :
SetProcessorAffinity( ref hthread, int mask) ?????
Here is how you can do it.
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
namespace Tester
{
class Program
{
[DllImport("kernel32")]
static extern int GetCurrentThreadId();
static void Main()
{
Thread t = new Thread(
new ThreadStart(DoWork));
t.Start();
t.Join();
}
static void DoWork()
{
foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
int utid = GetCurrentThreadId();
Does GetCurrentThread return the same ID each time in this loop? I heard
that .NET threads are logical threads, not physical threads. So
technically, wouldn't it be possible that another physical thread starts
executing the logical thread?
if (utid == pt.Id)
{
pt.ProcessorAffinity = (IntPtr)(1); // Set affinity for this
thread to CPU #1
Console.WriteLine("Set");
}
}
}
}
}
Willy.
Kind regards,
Tom,
I'm not using any Logical thread Id here, both 'GetCurrentThreadId' and
'pt.Id ' return the OS thread id. I'm using the first to get the current
OS thread ID and use the ID to search the corresponding thread in the
ProcessThreadCollection, once I have the ProcessThread I can use
ProcessorAffinity to set the affinity mask. As long as the thread runs it
keeps the same Id (and the same logical thread which I don't care).
I ran this on an 8 way box, funny how you can disturb the system by
playing with this

). IMO this API (ProcessorAffinity) shouldnt be used
from managed code, honestly I don't see why it was included in the
framework, while other more important features are still missing, cheep to
implement I guess.
Willy.
What I meant is that .NET has logical threads, the OS has physical threads
(if I read that correctly). Logical threads are executed on physical
threads, but I don't know if they need to execute on the same physical
thread each time the logical thread's time-slice phases in.
Actually I don't know the real details about how .NET framework implements
threading on WinOS. But if the logical thread can be executed by different
physical threads, then if the logical thread phases out right after the call
to GetCurrentThreadId, the logical thread could be rescheduled on a
different physical thread so the ID you got is no longer the correct one.
This is just my train of thought now, considering the difference between
physical and logical threads.
A sketch of my thoughts
// --> logical .NET thread [L_X] scheduled on physical WinOS thread [P_X]
int utId = GetCurrentThreadId( ); // utId == P_X
// --> the logical thread ends its time slice
....
// --> logical .NET thread [L_X] scheduled on physical WinOS thread [P_Y]
if (utId == pt.Id) // utId != P_Y
I'm not saying that this is possible, as I said I know too little of the
internal details. I think that the logical and the physical threads are
tightly coupled in the implementation of MS's .NET framework, but should
they be? Why did they make the distinction between logical and physical
threads otherwise? Does the documentation say that a logical thread is
always scheduled on the same physical thread?
Kind regards,