调用listener.Accept()并发执行

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年3月26日 13:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/36231968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定