2

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.

  • imho, the affinity is such low, that the scheduler will "try to begin the action" but reverts that, because there are "better things to do" (wich a higher affinity)... only after 5-10seconds the scheduler finds a slot for your action... – TheHe Aug 26 '12 at 03:28
  • @TheHe I thought SetAffinity set's which core to use? What do you suggest to improve my threading? If I just create a new thread it lags, because 2 threads on 1 core isn't that nice. –  Aug 26 '12 at 03:59
  • aah.. 4sure you're right... i was @ priorities and something.. first: does the xbox have 4 processors?! second: check this -> http://msdn.microsoft.com/en-us/library/windows/desktop/ms684251(v=vs.85).aspx third: set the thread-priority lower? – TheHe Aug 26 '12 at 05:07
  • The Xbox only has 3 cores, and one of them (0, if my memory doesn't fail) is reserved for the console's OS, so setting affinity to 3 (fourth processor) would be useless. I'm not sure that's the problem, but try setting it to 1 or 2, or reading documentation regarding such stuff. – Elideb Aug 27 '12 at 07:46

1 Answers1

1

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.

Tom Lint
  • 483
  • 5
  • 13
  • Well, I did do exactly what you said, except without the while(true) loop, which is silly. –  Sep 05 '12 at 02:40
  • 1
    Not exactly silly. Creating & tearing-down threads is an expensive operation. Use an endless loop (I prefer for(;;)) and some synchronization primitives. – Simon Sep 05 '12 at 02:42