add arrow-down arrow-left arrow-right arrow-up authorcheckmark clipboard combo comment delete discord dots drag-handle dropdown-arrow errorfacebook history inbox instagram issuelink lock markup-bbcode markup-html markup-pcpp markup-cyclingbuilder markup-plain-text markup-reddit menu pin radio-button save search settings share star-empty star-full star-half switch successtag twitch twitter user warningwattage weight youtube

How does threads work?

UltraNoob123
  • 2 months ago

How does the amount of threads translate into how many processes a cpu can execute?

Comments

  • 2 months ago
  • 3 points

Depends on whether you're talking about software threads or hardware threads. On the software side, a process can be thought of as an address space plus one or more threads of control that all see that address space. On the hardware side, each core can run one thread; if the CPU has hyperthreading / SMT available, a core can run two threads, but only when the two threads aren't contending for the same core resource (like an adder or memory access unit), in which case one of the threads is stalled until the hardware resource is available.

So if you have a 6-core, 12-thread CPU like a Ryzen 3600 or i7-8700K, you can run 6 software threads independently at more or less full speed. (I'll ignore memory and cache contention effects.) Let's assume those 6 threads are banging away at their respective CPU cores, doing nothing but computation, no I/O. You can now run 6 more software threads and they'll run at something like 30% of full speed, depending on what's going on. Beyond that, you have to do context switching, where the operating system stops a thread from running, saves its transient state (registers and what-not), and starts a different software thread running.

In real life, most (software) threads aren't 100% compute bound. So, when they have to wait for I/O, it makes sense for the OS to context-switch in a different thread that is waiting to do computation.

  • 2 months ago
  • 2 points

A thread is a virtual version of a cpu core. In other words, it is not giving you more hardware to process things with, it is just using it better. If a core is split into 2 threads, and the 1 thread is 100% using that core then the 2nd thread doesn't really help much at all. But when that 1st thread does something then has to wait for some reason, then that 2nd thread can jump in and do something while the first is waiting. All this helps with performance and better utilization of the physical core.

  • 2 months ago
  • 1 point

There is a lot to read about that matter. ;)

https://en.wikipedia.org/wiki/Thread_(computing)

Sort

add arrow-down arrow-left arrow-right arrow-up authorcheckmark clipboard combo comment delete discord dots drag-handle dropdown-arrow errorfacebook history inbox instagram issuelink lock markup-bbcode markup-html markup-pcpp markup-cyclingbuilder markup-plain-text markup-reddit menu pin radio-button save search settings share star-empty star-full star-half switch successtag twitch twitter user warningwattage weight youtube