Multithreading – if there were more threads, would my program get more CPU time?

If the current kernel is scheduling 60 threads, they belong to 3 processes:

They are all doing calculations (no disk IO)

Will c get more than B? Does B do more than a?

It doesn't seem fair to me If I were an irresponsible programmer, I could generate more threads to occupy more CPU resources

What does this have to do with golang: in go, the go scheduler usually has a thread pool of #of CPU cores threads Why does it make sense if processes with more threads do more work?

Solution

You can also allocate the whole available memory, cause operating system failure and hijack the network card You can do all kinds of things, but who wants to use your software?

Golang goroutines is basically a thread pool Each goroutine is a thread pool work item Many things can block your thread pool threads, such as using synchronous IO, waiting for (non spin lock) locks and manually sleeping or giving in In these cases, this is very common. Having more threads than the CPU usually improves the performance of the application Note that not all IO is disk IO Writing to the console is an IO operation, but it is not actually "disk IO" Another thing is that context switching may not take up most of your CPU, and having more threads may not reduce your task throughput Therefore, having more threads in this case means you have more parallelism, but you won't lose performance This is a common situation Context switching between threads is very cheap these days Having more threads than the core may not necessarily kill your performance or degrade it in some way

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>