Home All Groups Group Topic Archive Search About

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

Author
8 Apr 2005 4:13 PM
kaiteriteri
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.}

Author
8 Apr 2005 5:44 PM
Herfried K. Wagner [MVP]
"kaiteriteri" <kaiterit***@discussions.microsoft.com> schrieb:
>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/en-us/cpref/html/frlrfSystemDiagnosticsProcessClassProcessorAffinityTopic.asp>

'ProcessThread.ProcessorAffinity'
<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessThreadClassProcessorAffinityTopic.asp>

'ProcessThread.IdealProcessor'
<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessThreadClassIdealProcessorTopic.asp>

Determining the number of processors:

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

..NET 2.0:  'Environment.ProcessorCount'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Apr 2005 7:31 PM
AMercer
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.

Show quoteHide quote
"kaiteriteri" wrote:

> 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.}
Author
9 Apr 2005 2:28 AM
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

Show quoteHide quote
"AMercer" <AMer***@discussions.microsoft.com> wrote in message
news:1E7CDD5B-B988-4238-AE66-3A5E3C89D900@microsoft.com...
>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.
>
> "kaiteriteri" wrote:
>
>> 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.}
Author
11 Apr 2005 2:13 PM
kaiteriteri
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...)