How to *force* threads to run on certain processors?

  • Thread starter Thread starter Jon B
  • Start date Start date
J

Jon B

Hi There!

We have 2 processor server (Not dual core, actual seperate processors).

Is it possible from .NET to force some threads to run on certain processor
(say on processor #2)? For instance, run UI stuffs on processor #1 and run
other background stuffs on processor #2.

Possible?

Thanks in advance,
Jon
 
Jon said:
Hi There!

We have 2 processor server (Not dual core, actual seperate
processors).
Is it possible from .NET to force some threads to run on certain
processor (say on processor #2)? For instance, run UI stuffs on
processor #1 and run other background stuffs on processor #2.

Possible?

From Win32, you can do this using SetThreadAffinityMask.

The CLR processing model does not guarantee that a .NET thread corresponds
to a Win32 thread, but in practice they do, so you could probably P/Invoke
to SetThreadAffinityMask and get something that "works", but that could be
broken by the CLR in some future release when/if .NET threads are no longer
1:1 associated with Win32 threads.

If you're hosting your code in SQL Server, this may already be the case (I
think that feature was pulled before 2005 shipped, but I don't know for
sure).

Generally, messing with thread affinity is a bad idea - it rarely improves
the performance of your application and can do it serious harm.

-cd
 
Carl Daniel said:
From Win32, you can do this using SetThreadAffinityMask.

The CLR processing model does not guarantee that a .NET thread corresponds
to a Win32 thread, but in practice they do, so you could probably P/Invoke
to SetThreadAffinityMask and get something that "works", but that could be
broken by the CLR in some future release when/if .NET threads are no
longer 1:1 associated with Win32 threads.

If you're hosting your code in SQL Server, this may already be the case (I
think that feature was pulled before 2005 shipped, but I don't know for
sure).

Generally, messing with thread affinity is a bad idea - it rarely improves
the performance of your application and can do it serious harm.

-cd


Note that you don't need to use PInvoke for this, you can assign a thread to
a specific processor by means of the ProcessorAffinity property of the
System.Diagnostics.ProcessThread class.
But as you said, this is a bad idea, the OS will try to keep the UI threads
on the processor they were initially created on anyway

Willy.
 
Back
Top