How do you force a thread to run on a specific processor?

G

Guest

I have a time-consuming VB.net application that i'd like to thread over
2 processors (that's all i've got in my machine!) and, hopefully, get it
done in
half the time. On running, the application should create a 2nd thread and run
it on the other processor (processing a distinct set of data), leaving the
current
thread to run and process its set of data. But the 2nd thread must run on the
free processor, otherwise there's no point...

Can someone point me towards a VB.net example that does this?

{I see mention elsewhere of Get/Set of AffinityMasks within Threads but not
any examples of its usage for this type of thing, I believe this is the
property
that is used to force a specific processor to be used.}
 
H

Herfried K. Wagner [MVP]

kaiteriteri said:
I have a time-consuming VB.net application that i'd like to thread over
2 processors (that's all i've got in my machine!) and, hopefully, get it
done in
half the time. On running, the application should create a 2nd thread and
run
it on the other processor (processing a distinct set of data), leaving the
current
thread to run and process its set of data. But the 2nd thread must run on
the
free processor, otherwise there's no point...

Some links on this topic:

'Process.ProcessorAffinity'
<URL:http://msdn.microsoft.com/library/e...nosticsProcessClassProcessorAffinityTopic.asp>

'ProcessThread.ProcessorAffinity'
<URL:http://msdn.microsoft.com/library/e...sProcessThreadClassProcessorAffinityTopic.asp>

'ProcessThread.IdealProcessor'
<URL:http://msdn.microsoft.com/library/e...ticsProcessThreadClassIdealProcessorTopic.asp>

Determining the number of processors:

\\\
Imports System
..
..
..
.... = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")
///

..NET 2.0: 'Environment.ProcessorCount'.
 
G

Guest

I don't think you have to do anything special to get what you want. If you
have two processors and you launch a second thread, windows will efficiently
allocate threads to processers. It will do so if your 2-thread program is
all that is running or if your 2-thread program runs concurrently with other
cpu hungry programs.
 
L

lgbjr

I would agree. Some months ago, I was actually preparing to do the same
thing (assigning threads to CPUs). I wrote all of the code to check the
number of CPUs, setup a realtime graph that shows the load for each
processor that's available (engineers love pretty graphs, espcially when
they move!), and was in the process of writing an algorithm to balance the
CPU load (at least for the threads in my app), when I noticed that the OS
does a reasonable job of balancing the load automatically.

I was actually happy to stop work on the balancing algorithm, because I was
having trouble getting my head around how to move an active thread from one
processor to another processor if the user started other CPU intensive apps
that I had no control over.

HTH
Lee
 
G

Guest

thx, i will try running multiple copies and see how i get on
before delving into the innards...

(thx to Herfried for the disection details...)
 

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

Top