Set a CPU for Thread in

Posted on 2009-12-16
Last Modified: 2013-12-17

I would like to know how can I specify a CPU that whill run a specific thread?
I have 2 threads. I woukd that CPU1 runs the Thread1 and CPU1 runs Thread2 and my application is still running in my CPU0.
Question by:jr_barros_jr
    LVL 27

    Accepted Solution

    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:

    Additionally, VS 2010 is suppose to have this, or similar technology rolled into it.
    LVL 4

    Assisted Solution

    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:

    To quote a line from that last link on SetThreadAffinityMask:
    "In most cases, it is better to let the system select an available processor."
    LVL 33

    Expert Comment

    by:Todd Gerbert
    Found this bit of code in C# here:, 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)
        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
            myThreadProc.ProcessorAffinity = 1
                Console.WriteLine("Thread {0} running on processor 1", myThreadId)
        End Sub
    End Module

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now