Issue with Thread.SetAffinity and Threading via XNA on the Xbox 360
题
So I developed a very efficient collision detection system, but the problem is that it still cannot run on the main thread since it's too damn slow.
I tried setting up some threading, and if the thread ends, another thread is created.
if (doneCollisions)
{
PopulateGrid();
}
if (doneCollisions)
{
Thread thread = new Thread(new ThreadStart(CheckCollisionsGrid));
thread.Start();
}
void CheckCollisionsGrid()
{
Thread.CurrentThread.SetProcessorAffinity(3);
doneCollisions = false;
//Increments through all the grids.
.
.
.
doneCollisions = true;
}
Now I noticed some odd behaviour when debugging. When I called Thread.SetAffinity, it sort of jumped back to it over and over once or twice before finally starting to actually check the collisions.
And now my collisions are delayed by 5-10 seconds...
If anyone has insight please input some here.
解决方案
The XBOX 360 has 3 cores with 2 hardware threads per core, producing a total of 6 hardware threads. Threads 0 and 2 are reserved by the XNA Framework, leaving 1, 3, 4 and 5 for you to use.
As for the question, why are you creating a new thread after checking for collisions, to do exactly the same all over again? If you need to repeat the collision checking in a thread, simply put it inside a while(true)
loop to keep it going.