[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • 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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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