Threading on Xbox360 with XNA
Posted by redbeard on May 15, 2011
I discovered recently that threads on the Xbox are not managed or scheduled across CPU cores automatically. You must call SetProcessorAffinity from within the target thread, and that thread will run only on the specific core (hardware thread) from then onwards. I wrote some helper code at the top of my worker-thread management function (m_Threads is the list of potentially active thread objects). My “do thread work here” implementation just spins waiting for work to arrive in a queue.
private static void DoThreadWork(object threadObj) { Thread myThread = threadObj as Thread; #if XBOX // manually assign hardware thread affinity lock (m_Threads) { // avoid threads 0 & 2 (reserved for XNA), and thread 1 (main render thread) int[] cpuIds = new int[] { 5, 4, 3}; for (int n = 0; n < m_NumActiveThreads; ++n) { if (m_Threads[n] == myThread) { int cpu = cpuIds[n % cpuIds.Length]; myThread.SetProcessorAffinity(cpu); break; } } } #endif // do actual thread work here ...
Leave a Comment