Reputation: 327
Consider a golang program running on a system with GOMAXPROCS value 10. Due to blocking system calls, OS spawns 30 more threads resulting in 40 OS threads attached to the process.
After all the blocked system calls returns, then will the process still be having 40 OS threads? If yes, then can we conclude that the number of OS threads mapped to a golang process can grow but never comes down?
Upvotes: 3
Views: 709
Reputation: 417682
Yes, currently threads spawned due to blocked goroutines are not stopped. There's a discussion about closing idle threads periodically: runtime: let idle OS threads exit #14592
There is a way to kill a thread though. If you call runtime.LockOSThread()
in a goroutine without calling its counterpart runtime.UnlockOSThread()
, as per the doc:
If the calling goroutine exits without unlocking the thread, the thread will be terminated.
You may also do it using (source: runtime: terminate locked OS thread if its goroutine exits #20395):
syscall.Syscall(syscall.SYS_EXIT, 0, 0, 0)
Upvotes: 3