On Mon, Jun 20, 2022 at 3:27 PM Rahul Tiwari <
jprrahu...@gmail.com> wrote:
>
> I have intentionally set a max limit on the number of threads which can be spawned by my Go program to be 1 and the Go runtime errors out that it exceeds 1-thread limit.
> I've checked the entire code of runtime but couldn't find how the minimum threshold of threads to be spawned is decided. Any pointers?
There is no minimum threshold of threads as such. But the scheduler
will at the very least start a new thread to run the system monitor,
which runs the sysmon function in runtime/proc.go. So 2 is a absolute
minimum for the number of threads. And if a thread blocks in a system
call, and there are other goroutines waiting to run, the system
monitor thread will start another thread to run those goroutines.
This decision to start a new thread does not look at the limit set by
runtime/debug.SetMaxThreads. The SetMaxThreads function is intended
to stop a runaway program from taking down the entire system. It's
not intended to limit a Go program to using a very small number of
threads. Go programs are always inherently multi-threaded.
Ian