Search code examples
c#affinity

Difference between Process.ProcessorAffinity and Thread.ProcessorAffinity


If my application starts a process like this:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "Notepad.exe";
Process proc = Process.Start(psi);
proc.ProcessorAffinity = new IntPtr(1); 

How is it different to this:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "Notepad.exe";
Process proc = Process.Start(psi);
var threads = proc.Threads;

foreach (var thread in threads)
{
    thread.ProcessorAffinity = new IntPtr(1);
}

In each case, where do the processes run?


Solution

  • If you set the ProcessorAffinity at the process level, all the process' threads will inherit that affinity setting. From MSDN

    A bitmask representing the processors that the threads in the associated process can run on.

    So the difference between the two is that if you assign affinity at the thread level, you can assign them to more than one processor and spread the load more than with all threads assigned to one processor.

    Regarding your last point, "where are the processes run?", it's not the processes that are run, it's the threads and I think the answer is clear from the above.