Multithreading and multicore differences

53,673

Solution 1

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.

Solution 2

The number of cores and number of threads are decoupled. You can have many threads running on a single core, and you can have situations where only one thread runs despite the presence of more cores (although I cannot think of a real life scenario where this would happen). Let's say multicore is a hardware characteristic, whereas the number of threads is something in the domain of the OS and the processes running on it.

Of course, with a single core you cannot have more than one thread running concurrently. The OS has to constantly switch back and forth between threads.

Solution 3

Most cores have two threads

Here, I think you are confusing the overloaded term "thread". As correctly pointed out by other answers, a thread usually refers to a "software" concept. But sometimes it is also used as a "hardware" concept. When a "core" has two "threads" (like in many new Intel chips), it means that the core can run two parallel threads, as if there were two cores. This is, however, usually called hyperthreading. See:

http://en.wikipedia.org/wiki/Hyper-threading

So if you have N threads (I mean software threads, those made within your application, or just by running different applications at the same time) and M processors (being cores, or the hardware threads explained above), the following happens:

  • N<=M: then the Operating System should assign each thread a different processor. Then the application threads run truly in parallel.
  • N>M: then some threads have to share a processor.

Solution 4

Threading on a single core usually means you can create x number of threads and they will each be given set amount of time to run (thread quantum). When threads are switched this is known as context switching, all this takes some time so one needs to do some benchmarking to find an ideal number of threads to have per core.

If the majority of work is cpu bound there is little point spawning hundreds of threads since this is unlikely to improve performance (in-fact make it worse maybe, remember context switching does not come for free). However doing this for I/O bound work it may help since while the system is busy doing this work, another thread can be given cpu time.

Having physical additional cores means two things can truly run in parallel at the hardware level.

Share:
53,673
DavidColson
Author by

DavidColson

A programmer, game developer and physicist with an obsession with space.

Updated on December 05, 2020

Comments

  • DavidColson
    DavidColson over 3 years

    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

  • Inisheer
    Inisheer almost 12 years
    Just to add. A core can run multiple threads which seems to be running at the same time. However, as juanchopanza states, the CPU is actually switching back and forth between threads at an extremely fast rate. This makes it appears as if everything is running concurrently.
  • Jesse Good
    Jesse Good almost 12 years
    To be pedantic, concurrency and parallelism are two different things. So, you can have more than one thread running concurrently on a single core.
  • Mooing Duck
    Mooing Duck almost 12 years
    multiple threads are still handy without multiple cores, and (I believe) multiple cores are handy without multiple threads. Your answer implies ottherwise. Also, a mention of multitasking might not go amiss.
  • Mooing Duck
    Mooing Duck almost 12 years
    also, hyperthreading allows two threads(processes?) to run intermixed at a very fine level in a single core.
  • Wug
    Wug almost 12 years
    The last bit covers multiple threads without multiple cores. Multiple cores without multiple threads isn't handy, in fact it's totally useless. Thankfully, modern OSs have enough to busy themselves with that there is never only one process with one thread running, if there were, one core would be used and all of the others would sit idle.
  • Mooing Duck
    Mooing Duck almost 12 years
    Can't each core run a seperate process, or are they all tied to the same process?
  • Wug
    Wug almost 12 years
    They can, but if there's only one thread (total, in the whole system), the other cores are just expensive heaters. :)
  • Mooing Duck
    Mooing Duck almost 12 years
    Well, yes. I did make the assumption that there was more than one thread in the whole system :D Side note, I believe modern CPUs draw very little power when idle, nor create much heat.
  • Wug
    Wug almost 12 years
    probably not. expensive crappy heaters then
  • codarrior
    codarrior almost 5 years
    In a related context, have a look at docs.aws.amazon.com/AWSEC2/latest/UserGuide/… where vCPU represent a hyper-threaded core that allows this feature to be implemented in EC2.
  • Ηλεκτρολόγος Μηχανικός
    Ηλεκτρολόγος Μηχανικός about 3 years
    You can run it concurrently but you can't run it parallelly
  • Jin Lim
    Jin Lim over 2 years
    let's say, I created another new Thread from Main Thread. to do something. and let's assume we have 2 core (cpu) in the machine. Can we guarantee that Main thread is working by core1, and new Thread is working by core 2?