F
F
footballer2017-08-09 12:43:47
Programming
footballer, 2017-08-09 12:43:47

What is the point of running more than X threads on an X-core processor?

Because one core at 1 point in time can process only 1 operation, then at least break the program into threads, at least don’t break it - this means that the program will still not run faster, right? Let's say I need to write a program to calculate 2 heavy operations, each calculation takes 10 seconds. If I run the calculation of both operations sequentially in the same thread, then the first operation will be calculated and its result will be displayed in 10 seconds, and the result of the second will be displayed in another 10 seconds (both results will be ready in 20 seconds in total). If I create a second thread to calculate the second operation in it, then the OS will constantly switch between these two threads, the second thread will constantly interrupt the first, and the first - the second, as a result, both the first and second results will be calculated and displayed only after 20 seconds, both (i.e. it turns out
The concept of threads did not appear recently, but a very long time ago, I think that back in the 20th century, but in the mid-2000s, computers with single-core processors were still being sold. And I think that already in the mid-2000s, many programs were written that launched a bunch of threads, and executed these programs on single-core processors. The question is what is the meaning of this?
Even now, as a programmer, I see in the source codes, and simply on developer resources, pieces of code where a bunch of threads are launched (sometimes more than 10 threads). But now computers with 2, 4, 8 cores (maybe there are 16, but they are definitely less than 1%). Again, the question is - what is the point of running a program on a dual-core process that creates more than 2 threads?
The only situation that I still understand is when the second thread (heavy) is created in order not to hang the UI thread (in this case, constant switching to the UI thread will slow down the execution of the heavy thread, but at least the UI will at least respond on user actions). But why are a bunch of threads created even when there is no UI?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Neyury, 2017-08-09
@Neyury

The simplest example that uses multiple threads is networking.
If there is only one thread, and it requests data from the network, then it will stop its execution until the data arrives. If there are several threads, then while one is waiting for data from the network, the other can safely continue to work. In addition to working with the network, there are also read and write operations, which can be slow in some cases.

�
âš¡ Kotobotov âš¡, 2017-08-09
@angrySCV

yes, the efficiency drops when switching threads, BUT it is important for us to execute tasks in parallel, for example, you move the mouse over the screen - this is a separate process that works and should work in parallel.
Again, you need to understand that one thread cannot be executed efficiently due to delays -> as already mentioned, one of the longest delays when accessing the network or hard drive, until the data comes from there, you can switch the task without losing performance.
it should be remembered that, for example, the arrival of data from RAM also has its own delays, and if and while the next portion of data from RAM arrives, you can also switch to the process in which the data has already arrived, and so on.

E
evgeniy_lm, 2017-08-09
@evgeniy_lm

The meaning of multithreading is multitasking. It was multithreading that made it possible to run multiple applications simultaneously on a single-core processor.
Don't forget that every application, every service has at least one thread. But nevertheless, even on single-core processors, you can run multitasking OS since the days of 386 processors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question