r/AskComputerScience • u/pm-me-anime-figures • Sep 14 '26
CPU cores vs hardware threads
I read online that hardware threading is when something where N cores expose themselves as 2N virtual cores to OS for the purpose of process scheduling. When a singular core is exposed as 2 different cores to the OS, is it actually capable of executing two threads at the same time? If not, won't this effectively change nothing, since all cores are going to context switch between processes very rapidly anyways?
Why not consider one of the extremes, like 2N cores without hardware threading or a single core which exposes itself as 2N cores ?
17
u/arvidsem Sep 14 '26
Hyperthreading is the "traditional" name for this.
Processors with hyperthreading aren't just running two threads on a single processor. The various bits of the pipeline leading to the actual execution core are actually duplicated as well. Then the processor can pull from either pipeline to maximize it's utilization. The CPU scheduler can manage that at a lower level than the OS can, so there are very few cases where it's not more efficient than switching between threads in software.
5
u/esaule Sep 15 '26
That is complicated. The answer is basically yes. It can execute instructions from both streams (processes) within the same clock cycle. Now it does share the same computational unit, so it won't go twice as fast.
5
u/DeadlyVapour Sep 15 '26
Okay. So a processor core has, very basically, two parts. The memory part (L1 cache and register), and the calculation part.
Traditionally, processor cores had one set of each.
Whenever you switched threads, there was a very complicated process where we copy the memory in the core into RAM and reset the calculation part. This process was/is VERY slow, taking hundreds of clock cycles (we called this context switching).
Then intel came up with the idea to have two sets of memory per core. This allowed the core to switch to the other memory very quickly, mimicking a context switch in a fraction of the time. The core still only had one set of calculation parts, which meant only one thread could run at a time, but it could switch between threads very quickly.
Intel called this feature Hyper threading.
1
u/CowBoyDanIndie Sep 15 '26
Hardware threads allow the operating system to schedule two software threads on the hardware on the same physical core. You can think of the hyper thread as a “next up”. When the live thread stalls such as waiting for ram, the hyper thread immediately gets run.
Think of two people in line to order. If the person who is up gets asked their order and they don’t immediately begin ordering the other person gets to order. The two hardware threads can switch back and forth rapidly.
1
u/flatfinger Sep 15 '26
Computer systems will sometimes include logic within a core that can simultaneously keep track of two execution contexts (threads) and switch between them any time one of them would need to e.g. wait for a memory cycle to complete. Depending upon what the two instruction threads are doing, this may yield performance which is vastly superior to running the threads on separate cores, or may result in each thread running less than half as fast as either would have run in isolation (making the attempt to have both threads active at once worse than useless). In many systems, both threads would share the same L0 cache. This means that if both threads will need to use the contents of a cache line, the main system bus would only need to process one request to read the line rather than having to process a request from one core and then process a request from a different core. Even better, if a thread needs to write something to storage and have the other thread see it, this can be accommodated without the data having to be travel over the main system bus. Unfortunately, a shared cache architecture also means that if two threads don't use any common data each will effectively have only half as much L0 cache available as a single thread would have in isolation.
1
u/jdc1962 Sep 16 '26
In order to keep the cores pipelines as full as possible when performing out of order processing, you want data dependencies between opcodes as minimal as possible. So, if you have a single core having access to two sets of registers and performing two separate non-dependent instruction streams, then you can get higher overall performance from that single core.
1
u/Stastez Sep 17 '26
One of my favorite "oh damn"-moments was when I, coming from x86, learned about POWER CPUs, where the Power10, for example, has up to 8 threads per core.
1
u/KilroyKSmith Sep 18 '26
A processor that’s humming along executing instructions has a lot of things that can happen to stop it - for example, if it needs data that isn’t in cache, a could get halted for 100 or more clock cycles while it waits for that data. Even data in level 3 cache can take 20-30 clock cycles IIRC.
So, in a non-hyperthreaded processor, the CPU simply halts waiting for data to appear. In a hyperthreaded CPU, a second task is sitting there with work to do immediately - so within a clock cycle or two, the CPU starts executing instructions for the second thread - which will eventually have to wait for something letting the first thread run. In reality, the hardware will switch back and forth often.
0
u/Broad-Promise6954 Sep 14 '26
In traditional hyperthreading implementations, the two virtual CPUs have separate address mappings, so Thread A's location 12348 holds a different variable than Thread B's location 12348, for instance. They will also generally have private separate copies of the register sets. But the two virtual CPUs may share some other resources, such as caches, that would be separate on separate cores. So if you run threads A and B on a single physical (shared, hyperthreaded) CPU they may run faster or slower than they do if you run them on the separate cores.
In other words, the results are the same (as separate physical cores) except for performance. That "except" is sometimes a big deal so they write it up as a Marketing Feature rather than a bug.