小编典典

golang运行时:无法创建新的OS线程(已经有2049; errno = 12)

go

我在MacO上创建了许多goroutine,并且在执行程序时发出错误。

goRoutineId = 3710, i = 3683, len(chan) = 2049
runtime: failed to create new OS thread (have 2049 already; errno=12)
fatal error: runtime.newosproc

因此,我想知道“无法创建新的OS线程”是什么,就是golang的操作系统限制无法创建更多goroutine?感谢你们对我的帮助。


阅读 1384

收藏
2020-07-02

共1个答案

小编典典

这是操作系统的限制。我假设您正在使用linux。

根据go的来源,它是调用clone系统调用

ret := clone(cloneFlags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(funcPC(mstart)))
sigprocmask(_SIG_SETMASK, &oset, nil)

if ret < 0 {
    print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n")
    if ret == -_EAGAIN {
        println("runtime: may need to increase max user processes (ulimit -u)")
    }
    throw("newosproc")
}

clone(2)联机帮助页中,当时errno=12,错误原因是内存不足

ENOMEM Cannot allocate sufficient memory to allocate a task structure
              for the child, or to copy those parts of the caller's context
              that need to be copied.
2020-07-02