how to create 2 threads on on diferent procesors
-
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
-
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
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, 2001modified on Wednesday, December 23, 2009 10:49 AM
-
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
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.
-
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 -
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, 2001modified on Wednesday, December 23, 2009 10:49 AM
-
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 -
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: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 -
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, 2001modified on Wednesday, December 23, 2009 10:49 AM
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:
-
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
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