为什么conn read方法中有读锁?

prr*_*prr -2 go

在golang net包https://github.com/golang/go/blob/master/src/internal/poll/fd_unix.go#L140中 有一个读锁。有人能解释一下这把锁的用途吗?据推测,客户端和服务器之间的每个连接都是唯一的,因此不应期望来自不同线程的多次读取尝试?我还假设每个连接都有一个 go 例程处理程序。

 func (fd *netFD) Read(p []byte) (n int, err error) {
    if err := fd.readLock(); err != nil {
        return 0, err
    }
    defer fd.readUnlock()
Run Code Online (Sandbox Code Playgroud)

无锁提高效率

Pet*_*ter 6

我还假设每个连接都有一个 goroutine 处理程序。

这种假设是不合理的。在多个 goroutine 中使用连接是明确允许的:

多个 goroutine 可以同时调用 Conn 上的方法。

https://pkg.go.dev/net#Conn