What is the relationship between threads (in Java or C ++ programs) and the number of cores in a CPU?

Can someone shed light on him?

i7 can run 8 threads, but I'm sure we can make more than 8 threads in JAVA or C ++ (not sure though). I have i5, and while studying concurrency, I made 10 threads for assignments. I'm just trying to understand how the Core rating of a CPU is related to threads.

+4
source share
5 answers

The thread you are referring to is called software thread ; and you can create as many program threads as you need, if your operating system allows it. Each program stream or piece of code can run simultaneously from others.

There is at least one hardware thread for each core to which the operating system can assign a software thread. For example, if you have 8 cores, you have a pool of hardware threads with a throughput of 8. In this 8-slot pool, you can match tens or hundreds of software threads in which only 8 threads actually work at the hardware level, i.e. in parallel .

Software streams are similar to people who use the same computer. Everyone can use this computer for some time, it is not necessary to carry out their task, and then give it to another.

Hardware streams are similar to people who have a computer for each of them. All of them can simultaneously perform their tasks.

Note. For i7, there are two hardware threads in each core (the so-called hyperthreading). This way you can have up to 16 threads running in parallel.

+7
source

There are already some good answers about the hardware side of things, but there is not much to talk about the software side.

The essential fact, which I think you are missing, is that not all threads should be running all the time. When you have thousands of threads on an 8-core computer, only some of them actually work at any given time. The rest sit idle until some time the processor becomes free. This has huge advantages because threads can wait on other resources. For example, if I have one thread trying to read a file from disk, then there is no reason for it to load CPU time while it waits for data to be loaded onto the hard drive in RAM. Another example is that a thread is waiting for a response from some other machine (such as a web request over the Internet). When you have more threads than the processor can process right away, the operating system and / or the runtime environment (depends on the OS and implementation implementation). Responsibility for deciding which threads should receive the available processor time. This tuning allows you to maximize the performance of your computer, because the CPU cycles do something useful almost all the time.

+2
source

"thread" is a software abstraction that defines a single, self-consistent execution path through a program: in most modern systems, the number of threads is generally limited only by memory. However, only a relatively small number of threads can be started by the processor at a time. Generally speaking, β€œkernel counting” is the number of threads that a processor can run truly in parallel: if there are more threads to start than there are available kernels, the operating system will use some sort of time sorting so that all threads get some time for fulfillment.

There are a bunch of terms that rush around when it comes to "cores:"

  • Number of processors: the number of physical CPU chips on the system motherboard. This was the only number that mattered until multi-core processors became available.
  • Number of logical cores: the number of threads that hardware can run in parallel.
  • Physical kernel count: the number of copies of the processor hardware that the system has is not always equal to the logical count of the kernel, thanks to features like SMT ("simultaneous multithreading") that use a single piece of hardware to run multiple threads in parallel.
  • Number of modules: the latest (based on Bulldozer) AMD processors used an architecture that is a hybrid between the SMT and the standard model with one logical core per physical core. These processors have a separate copy of the integer execution equipment for each logical core, but the two logical cores share a floating point module and an I / O and decoding interface; AMD calls a module containing two logical cores a module.

It should also be briefly mentioned that graphics processors, graphics cards, have a huge number of cores and run huge numbers (thousands) of threads in parallel. The tradeoff is that GPU cores have very little memory and often have a significantly limited programming model.

+1
source

Threads are handled by the OS scheduler. The number of cores in a processor determines how many threads it can run at the same time.

Note that threads are constantly turning on and off the scheduler to give the "illusion" that everything works at the same time.

Read more here if you are interested.

0
source

no, no, no ... Your I7 has eight threads of execution and can run 8 threads at once.

1000 threads or more can wait for processor time.

calling thread.sleep moves the thread from the execution kernel and back to memory, where it waits until it wakes up.

0
source

Source: https://habr.com/ru/post/1497351/


All Articles