• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

Set a CPU for Thread in VB.net

Hi,

I would like to know how can I specify a CPU that whill run a specific thread?
Scenario:
I have 2 threads. I woukd that CPU1 runs the Thread1 and CPU1 runs Thread2 and my application is still running in my CPU0.
0
jr_barros_jr
Asked:
jr_barros_jr
2 Solutions
 
VBRocksCommented:
I'm not sure how easy it is to do that.  However, check out the Intel Parallel Studio.  MSDN just sent their subscribers an evaluation CD.  The Intel Parallel Studio is supposed to help programmers program against multiple processors.  Here's a link:

    http://software.intel.com/en-us/intel-parallel-studio-home/

Additionally, VS 2010 is suppose to have this, or similar technology rolled into it.
0
 
nfashawCommented:
When using multiple threads, I'd suggest leaving to the operating system to determine on which processor/core they run.  But if you're really interested, check out:

http://msdn.microsoft.com/en-us/library/system.threading.thread.beginthreadaffinity(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms686247(VS.85).aspx

To quote a line from that last link on SetThreadAffinityMask:
"In most cases, it is better to let the system select an available processor."
0
 
Todd GerbertIT ConsultantCommented:
Found this bit of code in C# here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a55733db-cac0-4ccb-a3cc-a584742b41f9/, seems to test okay.
Imports System.Threading
Imports System.Runtime.InteropServices
Module Module1
    <DllImport("kernel32")> _
    Public Function GetCurrentThreadId() As Integer
    End Function
 
    Sub Main()
        Dim thrd As New Thread(AddressOf ThreadProc)
 
        thrd.Start()
        thrd.Join(20000)
    End Sub
    Sub ThreadProc()
        Dim myThreadProc As ProcessThread = Nothing
        Dim myThreadId As Integer = GetCurrentThreadId()
 
        For Each pt As ProcessThread In Process.GetCurrentProcess().Threads
            If pt.Id = myThreadId Then
                myThreadProc = pt
                Exit For
            End If
        Next
 
        myThreadProc.ProcessorAffinity = 1
 
        Do
            Console.WriteLine("Thread {0} running on processor 1", myThreadId)
            Thread.Sleep(1000)
        Loop
    End Sub
End Module

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now