XNA Multithreading: Must Set Processor Affinity Manually

\<a href="http://www.knowing.net/images/XNAMultithreadingMustSetProcessorAffinit_7800/image011.png"" atomicselection="true">

Ah, that's more like it. This is a graph of duration (in seconds) versus the number of threads during a calculation of a Mandelbrot set using the XNA Framework on the XBox 360 (Neil: The previous graph was normalized speed, as you suspected. For more on labeling the Axis in Office 12, see the next post.)

In order to have the XNA Framework distribute processing, you must explicitly set the "thread affinity." This must be done within the worker thread (presumably early in the ThreadStart delegate) and, on the XBox 360, you must not use the values 0 or 2, which are reserved. Thus, I use code like this:

struct ThreadWorker

{

    int processorAffinity;

    static int processorAffinityIterator = 0;

    internal ThreadWorker()

    {

        switch (processorAffinityIterator)

        {

            case 0  :

                processorAffinity = 1;

                break;

            case 1 :

                processorAffinity = 3;

                break;

            case 2 :

                processorAffinity = 4;

                break;

            case 3 :

                processorAffinity = 5;

                break;

            default :

                processorAffinity = 1;

                break;

        }

        if (++processorAffinityIterator == 4) { processorAffinityIterator = 0; }

    }

   internal void ThreadRun()

   {

   Thread.CurrentThread.SetProcessorAffinity(processorAffinity);

   ... begin work ...