Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are applications run on multi-core machines?

I'm trying to gain a better understanding of how multi-core processors work and how as a programmer I can take advantage of them.

Let's say I have a standard .net console app. It doesn't do any multithreading. Does it only run on 1 of the cores? If so which core does it run on and is it the same one everytime?

Now let's say I have another console app that spins up a bunch of threads internally. Do the threads get divided up amongst the available cores or all they all run on the same core the the initial thread is on and I have to do something special to use the other available ones?

like image 470
Micah Avatar asked Sep 28 '10 21:09

Micah


People also ask

Which applications use multiple cores?

The following are examples of CPU-hungry applications that can take advantage of multiple cores: Photo and video editing apps— Adobe Photoshop, Adobe Premier, iMovie. 3D modeling and rendering programs — AutoCAD, Solidworks. Graphics-intensive games — Overwatch, Star Wars Battlefront.

Can one program run on multiple cores?

Yes, a single process can run multiple threads on different cores. Caching is specific to the hardware. Many modern Intel processors have three layers of caching, where the last level cache is shared across cores.

What applications benefit from multi-core architecture?

Various industrial areas such as military, transportation, automation and control etc work with multicore systems because of their intensive advantages. As mentioned earlier, application of multicore systems improve upon the performance of the system with high processing speed, less power consumption etc.

What is a multi-core operating system?

Multicore. Definition. It is a system with multiple CPUs that allows processing programs simultaneously. A multicore processor is a single processor that contains multiple independent processing units known as cores that may read and execute program instructions. Execution.


1 Answers

The relationship between cores, threads, processes, and classes can be a complicated one. More so when you start considering how code you may not have written (e.g. the .NET framework itself) is scheduled to run.

The simplistic answer is that unless you specifically write your console application to use multiple threads, it will run on a single core.

Now, there are some .NET framework classes that behind the scenes may use multiple threads, in which case your process may utilize more than one core even then. Much of the BCL doesn't use threads, but frameworks like WCF and WF may internally use threading.

Furthermore, the runtime environment may uses multiple threads (in some instances) for things like garbage collection. Beyond even that, the runtime environment may move your code from core to core. This occurs because the OS itself uses pre-emptive multitasking to schedule threads - and so a single thread isn't guaranteed to have affinity to a particular core.

For the second part of your question, if a process spawn threads explicitly, they will usually (but not always), end up on separate cores. Most of the time, these threads will be scheduled to run on whichever core has capacity - and threads (as I mentioned earlier) may move from core to core. You should not have to do anything special (usually) to get multiple cores to run your code.

It's actually the inverse that is sometimes harder to achieve: ensuring that a particular thread only runs on a single core. This is referred to as affinity. In certain cases it is desirable to affinitize a thread to a particular core to improve locality of reference and minimize potential for cache misses. Most of the time, however, you don't need to worry about this.

If you're interested in learning more about concurrent programming on Windows (and in .NET) I would suggest you pick up a copy of Joe Duffy's Concurrent Programming on Windows.

like image 196
LBushkin Avatar answered Nov 15 '22 15:11

LBushkin