Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will my program get more cpu time if it has more threads

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

A: 10 threads

B: 20 threads

C: 30 threads

And they are all doing computing (no disk IO)

Will C gets more stuff done than B, and B gets more stuff done than A?

It does not seem very fair to me. If I am a irresponsible programmer, I can just spawn more threads to eat more CPU resource.

How this relate to golang: In go, typically the go scheduler has a thread pool of #of-CPU-cores threads. Why does this make sense if a process with more threads gets more stuff done?

like image 279
Helin Wang Avatar asked Nov 09 '22 03:11

Helin Wang


1 Answers

The situation you describe is an overloaded machine. It is only true that the process with more threads will get more work done if the CPU has no spare time.

Go was not designed to fight other processes for a greater share of an overloaded CPU. You are free to set GOMAXPROCS to any number you like if you desire to participate in such a fight.

In a more typical system where the total work is less than the total CPU time; a Go process with 8 threads and 30 goroutines will perform about equally to a process with 30 threads running at the same time.

like image 110
Bracken Avatar answered Nov 15 '22 06:11

Bracken