By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,995 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,995 IT Pros & Developers. It's quick & easy.

Multi-Processor Threading

P: n/a
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;
Oct 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bob Allen wrote:
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
Oct 10 '07 #2

P: n/a
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...

--
Chris Mullins

"Bob Allen" <bo*@greatsummits.comwrote in message
news:eL**************@TK2MSFTNGP04.phx.gbl...
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;

Oct 10 '07 #3

P: n/a
On Oct 9, 11:24 pm, "Bob Allen" <b...@greatsummits.comwrote:
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;
Not without messing with thread affinity masks and threadpools
(answered elsewhere on this thread), but coming soon:

Parallel FX CTP
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Beautiful stuff!

- Oisin / x0n


Oct 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.