Golang net.Conn并行写入 [英] Golang net.Conn Write in parallel

查看:1256
本文介绍了Golang net.Conn并行写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个Goroutine共享一个net.Conn对象.他们可以同时发出写呼叫吗?

I have multiple Goroutines sharing a net.Conn object. Can they issue a Write calls simultaneously?

我主要关心的是部分完成的Write调用.假设我打算写100个字节,但只发送了30个字节,因此我需要再发送70个字节.为此,我通常会编写一个循环:

My main concern is that of Write calls which are partially done. Say I intend to wrote 100 bytes, but only 30 were sent, so I need to send 70 more. For this I will typically write a loop:

count := 0
for count < len(buf) {
    byteSent, err := conn.Write(buf[count:])
    //check error 

    count += byteSent
}

但是我看到Go在 net.Conn.Write 行号中实现了此循环318并通过锁定来做到这一点.

But I see that Go implements this loop in net.Conn.Write line number 318 and it does so by taking a lock.

但是,在 Windows 的实现中,没有这样的循环,只是有一个调用到WSASend.我不知道WSASend的行为如何,无法从MSDN文档中获得很多帮助

However, on Windows implementation there is no such loop except that there is a call to WSASend. I do not know how WSASend behaves and could not get much from the MSDN docs

因此,问题是:

[edit]添加了第四个问题

[edit] Added 4th question

  1. 每次写入套接字时都需要获取锁吗?
  2. 如果是,那么将无法实现Write实现中的锁定.
  3. 在unix实现中,是否表示我无法获得byteSent< len(buf)除非err!= nil? (我是说我读的代码正确吗?)
  4. Windows上的WSASend是否在Unix实现中实现了等效循环

推荐答案

  1. io.Write 表示在部分写入的情况下,err将为!= nil

  1. The io.Write says that in case of partial write, err will be != nil

此处找到了在StackOverflow上,WSASend不需要在其周围循环.

Found here on StackOverflow that WSASend need not have a loop around it.

从#1开始#2,这意味着我在调用net.Conn.Write之前不需要获取锁.

From #1 & #2, it implies that I need not acquire lock before calling net.Conn.Write.

所以我的问题得到了回答.

So my question stands answered.

这篇关于Golang net.Conn并行写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆