Multithreading and multicore differences

DavidColson picture DavidColson · Aug 6, 2012 · Viewed 45.7k times · Source

I have a couple of small questions.

Firstly is there a difference between multithreading and multicore? Are they two completely different things or does multithreading use more than one core if it needs?

Secondly Most cores have two threads but when profiling my app I noticed lots of different threads ranging from thread 128 to thread 3460. What dictates how many threads your computer has?

Thanks

Answer

Wug picture Wug · Aug 6, 2012

Firstly is there a difference between multithreading and multicore?

Yes.

Multithreading and Multicore are different pieces of terminology that apply to different areas of computing.

  • Multicore refers to a computer or processor that has more than one logical CPU core, and that can physically execute multiple instructions at the same time. A computer's "core count" is the total number of cores the computer has: computers may have multiple processors, each of which might have multiple cores; the core count is the total number of cores on all of the processors.

  • Multithreading refers to a program that can take advantage of a multicore computer by running on more than one core at the same time. In general, twice as many cores equals twice as much computing power (for programs that support multithreading) though some problems are limited by factors other than CPU usage; these problems will not experience gains that are as dramatic from multithreading.

    It's important to note that performance is not the only reason programs use many threads. More on that later.

Are they two completely different things or does multithreading use more than one core if it needs?

They are related but seperate.

Programs that support multithreading can use more than one core if more than one is available.

Most cores have two threads but when profiling my app I noticed lots of different threads ranging from thread 128 to thread 3460.

The operating system assigns threads numbers so it can keep track of them.

Most programs you will ever run will not need 3400 threads running at once. Also, a running thread will consume all of a core. The only reason your CPU isnt running at 100% all the time is that the operating system knows how to suspend the processor, which basically makes it stop everything and wait until something happens (such as an IO event or a clock tick). Only one thread can run on a core at once. Different threads running is actually just threads jumping onto the CPU and running for short periods of time, then being switched out with other threads which also need to run.

What dictates how many threads your computer has?

The total number of threads in all of your processes. Also, most operating systems impose a hard limit, a maximum number of existing threads that cannot be surpassed.

A process is a program (you probably know this). Multithreading is the process of having more than one thread per process (many processes will not make more than one thread because they do not have to). Windows does not have a hard limit on the number of threads you can create (at least, not since XP. Not going to say anything about w98 and previous), but of course the number of threads you can make is limited by how much memory you have.

You said some programs use multiple threads for reasons other than performance.

Sometimes it's nice to be able to multitask, even if not concurrently.

Sometimes programs need to do specific things at specific times. A commonly cited example is a program with a visible window. That program might be doing intense background number crunching, but it would benefit it if it could still respond to user events, like clicking buttons and resizing it. This can be accomplished with asynchronous processing, which will require your one thread to repeatedly check for GUI work to do at intervals, pause what it's doing, and handle the GUI for a while. Lots of things do it this way.

Another, possibly better way to handle it is with a thread. Your program doesn't have to worry about switching back and forth between number crunching and GUI management, the operating system will manage that for you. Even if you have only one core, you can still run multiple threads, and your OS will do its best to make sure that all of the running threads in all of the running processes get their fair share of CPU time.