Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to create 2 threads on on diferent procesors

how to create 2 threads on on diferent procesors

Scheduled Pinned Locked Moved C#
tutorialasp-nethelplounge
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Renven
    wrote on last edited by
    #1

    hi guys, I need your help. let's suppose I have Dual-Core CPU and I need know how to create two threads, each in different processor. for example: thread1 runs in 1core thread2 runs in 2core when implementing tread1.start(); thread2.start(); they both run some random function lake NumberTest(); Thanks in advance, renven

    realJSOPR G L 3 Replies Last reply
    0
    • R Renven

      hi guys, I need your help. let's suppose I have Dual-Core CPU and I need know how to create two threads, each in different processor. for example: thread1 runs in 1core thread2 runs in 2core when implementing tread1.start(); thread2.start(); they both run some random function lake NumberTest(); Thanks in advance, renven

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      Thread.SetProcessorAffinity()[^] It appears as if threading in .Net already distributes multiple threads across all available processors/cores, so you don't appear to have to worry about it.

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      modified on Wednesday, December 23, 2009 10:49 AM

      G L 2 Replies Last reply
      0
      • R Renven

        hi guys, I need your help. let's suppose I have Dual-Core CPU and I need know how to create two threads, each in different processor. for example: thread1 runs in 1core thread2 runs in 2core when implementing tread1.start(); thread2.start(); they both run some random function lake NumberTest(); Thanks in advance, renven

        G Offline
        G Offline
        Ghydo
        wrote on last edited by
        #3

        C# seems not to support the feature you are requiring, you should use some Interop to do it. This snippet should do the work (I found it here[^], I didn't tested it.

        class Program
        {
        [DllImport("kernel32")]
        static extern int GetCurrentThreadId();
        static void Main()
        {
        Thread t = new Thread(
        new ThreadStart(DoWork));
        t.Start();
        t.Join();
        }

        static void DoWork()
        {
        	foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
        	{
        		int utid = GetCurrentThreadId();
        		if (utid == pt.Id)
        		{
        			pt.ProcessorAffinity = (IntPtr)(1); // Set affinity for this thread to CPU #1
        			Console.WriteLine("Set");
        		}
        	}
        }
        

        }

        But why do you want to change the thread affinity? The scheduler should automatically take advantage of both CPU cores.

        1 Reply Last reply
        0
        • G Ghydo

          This is surely a better solution than mine! :)

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          Then vote it a good answer. :)

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          G 1 Reply Last reply
          0
          • realJSOPR realJSOP

            Thread.SetProcessorAffinity()[^] It appears as if threading in .Net already distributes multiple threads across all available processors/cores, so you don't appear to have to worry about it.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            modified on Wednesday, December 23, 2009 10:49 AM

            G Offline
            G Offline
            Ghydo
            wrote on last edited by
            #5

            This is surely a better solution than mine! :)

            realJSOPR 1 Reply Last reply
            0
            • realJSOPR realJSOP

              Then vote it a good answer. :)

              .45 ACP - because shooting twice is just silly
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

              G Offline
              G Offline
              Ghydo
              wrote on last edited by
              #6

              Wait, re-reading carefully the documentation it seems that it is only for XNA and XBox360. I tried a small test program and the Thread.SetProcessorAffinity does not exist in the standard 3.5 .NET framework. Am I missing something obvious? :confused:

              realJSOPR 1 Reply Last reply
              0
              • G Ghydo

                Wait, re-reading carefully the documentation it seems that it is only for XNA and XBox360. I tried a small test program and the Thread.SetProcessorAffinity does not exist in the standard 3.5 .NET framework. Am I missing something obvious? :confused:

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                You're right, and I changed my answer.

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                1 Reply Last reply
                0
                • realJSOPR realJSOP

                  Thread.SetProcessorAffinity()[^] It appears as if threading in .Net already distributes multiple threads across all available processors/cores, so you don't appear to have to worry about it.

                  .45 ACP - because shooting twice is just silly
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                  modified on Wednesday, December 23, 2009 10:49 AM

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Sounds more logical too; imagine all applications dividing their affinity over two cores. You'd freak out if your computer has four cores, as two would be consistently ignored :) The CPU-affinity can be set by the end-user over the task-manager, if there's a need to do so.

                  I are Troll :suss:

                  1 Reply Last reply
                  0
                  • R Renven

                    hi guys, I need your help. let's suppose I have Dual-Core CPU and I need know how to create two threads, each in different processor. for example: thread1 runs in 1core thread2 runs in 2core when implementing tread1.start(); thread2.start(); they both run some random function lake NumberTest(); Thanks in advance, renven

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, don't worry about which processor executes which thread; setting processor affinities isn't necessary at all. If you let everything run without "helping" it, Windows will assign threads to processors dynamically (which means a thread once blocked for some reason might continue on another processor), with some retention as to preserve cache efficiency. It is only in very special cases that you could improve on the automatic assignment. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups