Multi-Processor Threading

  • Thread starter Thread starter Bob Allen
  • Start date Start date
B

Bob Allen

Is there a way to specifiy what processor to run a thread on in a quad core
processor in c#. Any links or direction is greatly appreciated.

Bob;
 
Bob said:
Is there a way to specifiy what processor to run a thread on in a quad core
processor in c#. Any links or direction is greatly appreciated.

I don't know if you can do this using only managed code. However, you
should be able to accomplish it by using the
Thread.BeginThreadAffinity() method (to ensure that the managed thread
stays with a specific OS thread) and then using p/invoke to call the
unmanaged function SetThreadAffinityMask() (which is what will actually
assign the thread to a specific CPU core).

If you are okay with your code running only on the Xbox 360, you can use
the Thread.SetProcessorAffinity() method found in XNA. :)

If you could determine what thread is the current ProcessThread, that
class has a ProcessorAffinity property that you could use. Maybe
there's a way to do that, but if it is, it's either not obvious or I'm
too tired to find it. :)

Pete
 
This is something that you can do - just Google the method Peter already
replied with.

Honestly though, this is something you really, really don't want to be
doing. The O/S Thread Scheduler is better at scheduling threads than you
are, and leaving it the flexability to schedule as it sees fit is reall the
best way to go.

Unless you're doing something very unique, or have some hardware level
memory mapping going on, I would steer clear of this.

It's always tempting to people new to concurrency to start setting processor
thread affinity, just as it's often tempting to start setting thread
priorities. Doing this almost always leads to less performance and more
problems, and should be avoided at almost all costs.

If you're really desperate for more performance, download Richter's Power
Threading library, and use his I/O Completion Port thread pool. If nothing
else, reading through his code for a few hours will be a good learning
experience...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top