英文:
Calling listener.Accept() Concurrently
问题
假设我们正在监听一个 TCP 端口(例如使用 listener, err := net.Listen("tcp", ":8081")
),在不同的 goroutine 中同时调用 listener.Accept()
是可以的吗?这样做有助于最大化接受速度吗?
英文:
Assuming we are listening on a TCP port (using listener, err := net.Listen("tcp", ":8081")
for example), is it OK to call listener.Accept()
in different goroutines concurrently? Does it help with maximizing accepting speed?
答案1
得分: 4
net.Listener是底层的FileDescriptor。Accept()使用Plan9机制,在生成连接(newFD)的同时使用读锁(readLock)进行保护。所以看起来是安全的。而且由于锁是只读的,没有排他性,我认为甚至可以并发调用accept来提高速度。
英文:
net.Listener is a FileDescriptor under the hood. Accept() use Plan9 machinery which guards it with readLock while function and produce connection - newFD. So it looks safe. And because of Lock is read only, no exclusive, you even can get some speedup calling accept concurrently to my mind.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论