Will the number of OS threads spawned by go process never decrease?

ぃ、小莉子 提交于 2019-12-11 07:38:30

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/58008462/will-the-number-of-os-threads-spawned-by-go-process-never-decrease

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!