我在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?感谢你们对我的帮助。
这是操作系统的限制。我假设您正在使用linux。
根据go的来源,它是调用clone系统调用
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,错误原因是内存不足
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.