CPU & treads & Affinity RRS feed

  • Question

  • I am trying to get the CPU or the vCore that my tread was created on, but when I first try to get the Process by name

    I don't get anything back from : Process.GetProcessesByName

    Thread t = new Thread(new ThreadStart(Calculation))
                        Name = "thread_test_" + i
                    Process[] p=Process.GetProcessesByName("thread_test_" + i);

    on the last line I don't get anything, why?

    Tuesday, February 18, 2020 5:16 PM

All replies

  • A thread isn't a process.  A thread executes in the same process space that spawned it. 

    You'd need to query the System.Threading namespace (or the properties of the Thread t instance) to get information about that specific Thread.

    If you really want to use System.Diagnostics and Process for a homework assignment or something, then you need Process.GetCurrentProcess()

    If you wanted your thread to be a separate process, you'd have to create 2 separate executable projects and then use a Process instance to run/start that second executable from inside the first one.

    If you want to do any real multi-core programming including querying, you should be looking at Assembly or C along with the Windows API.

    Tuesday, February 18, 2020 5:49 PM
  • what i try to check under which CPU of vCores the 

    new threads will run?

    will all of them under the same process which run's on one thread or more?

    what happens in TPL?

    Tuesday, February 18, 2020 6:11 PM
  • Note that managed threads “[…] are distinct from the native threads provided by the operating system” []. “An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the Fiber API to schedule many managed threads against the same operating system thread, or to move a managed thread among different operating system threads” [].

    Therefore, not all the terms (e.g. “affinity”) are directly applicable to managed threads created by Thread class.

    • Edited by Viorel_MVP Tuesday, February 18, 2020 6:58 PM
    Tuesday, February 18, 2020 6:56 PM