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); t1.Start(); t2.Start();
are t1 and t2 threads guaranteed to run on separate cores?
You cannot guarantee in .Net that two
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
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.
No, the OS and CPU will decide what to run and when. in the simple example you have shown, to the exclusion of other tasks, yes those would most likely run in parallel on separate cores, but there is rarely a guarantee that that will be the case.
You can use the thread affinity to attempt to take some control over the allocation of a core to a given thread.
Also consider scheduling priorities to stack the deck in terms of which threads should be entirely parallel, and which can wait.