Creating One Thread Per Processor
suggest changeEnvironment.ProcessorCount Gets the number of logical processors on the current machine.
The operating system schedules those threads. They may land on different logical processors, on one processor, or on some mix of the two. Creating the threads does not pin each one to its own processor.
using System;
using System.Threading;
class MainClass {
static void Main() {
for (int i = 0; i < Environment.ProcessorCount; i++) {
var thread = new Thread(Secondary);
thread.Start(i);
}
}
static void Secondary(object threadNumber) {
System.Console.WriteLine("Hello World from thread: " + threadNumber);
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents