When you start multiple threads on a multi core processor, are they guaranteed to be processed by different cores


I have a Pentium core i5 processor, which has 4 cores. If I do this in a C# console program

var t1 = new Thread(Thread1);
var t2 = new Thread(Thread2);

are t1 and t2 threads guaranteed to run on separate cores?

Best Answer

You cannot guarantee in .Net that two Threads run on two separate cores. In fact, you also cannot guarantee that one Thread will run on only one core(!).

This is because managed threads are not the same as OS threads - a single managed Thread may use multiple OS threads to support it. In C#, you only ever deal directly with managed Threads (at least, without resorting to p/invoke to call the WinAPI threading functions, which you should never do).

However, the .Net and Windows thread schedulers are very good at what they do - they wouldn't run two threads on a single core while a second core sits completely idle. So, in general, you don't need to worry about it.