Xbox360 Threads, just interested ...
I know that the xbox has multi cores and can run several threads concurrently, how do you write for a particular core on the xbox 360, or will the CLR choose when to use another core. Or maybe our code will only run on 1, can someone please enlighten me. Hopefully, it will be as easy as this -
Thread myThread =newThread(newThreadStart(myProc));
myThread.SetApartmentState(ApartmentState.MTA)
myThread.Start();
and the clr does all the hard work. I am just interested thats all ...
Dave
In general, it's the job of the OS scheduler to spread threads across
cores. If multiple cores are available, and multiple threads are
running, an efficient scheduler will not bounce the threads between the
cores, because that would cause cache thrashing.
If you KNOW
that certain threads will work better when bound to certain cores, you
use the ::SetThreadAffinity() Win32 call, which is available as
Thread.SetProcessorAffinity() in .NET.
Note that the "apartment state" is not necessary to set; the default is what you want.
The Xbox thread scheduler is significantly different to the one in the Windows kernel.
On Windows, the OS does all sorts of smart stuff to load balance
between different threads and processes, so the usual advice is just to
kick off however many threads you need and let the OS figure out the
details.
On Xbox things are more manual. This can be both a good thing and a bad
thing: a good thing because you get more control and therefore more
deterministic performance, but a bad thing because you have to think a
bit more carefully about what you are doing. If you just create threads
and let them run in the default way, they will all execute on the same
CPU core, which is unlikely to be what you really wanted! To balance
threads across cores, you need to call
Thread.CurrentThread.SetProcessorAffinity. I seem to remember this has
to be called by the thread you want to change, ie. you can't alter the
affinity of some other thread, so typically this call will be the first
thing you do after creating the new thread.
I'm not sure exactly how many hardware threads will be available for
you to play with (I didn't work on that area of the code). I know we
reserved either one or two for framework use, leaving either four or
five for your own code, but don't remember which. Regardless, you
generally want to be thinking in terms of running three threads at
once: trying to use more than that can get you in trouble unless you
really understand what you are doing, since the hardware has three
independent CPU cores.