douxiji8707 2016-06-20 12:37
浏览 90

是否多个goroutine会同时在Conn上调用方法?

My program like this:

func handle(conn net.Conn) {
    msg := "hello, world!"
    for i:= 0; i< 100000; i++ {
        go func() {
            err := write(conn, msg)
        }
    }

}
func write(conn net.Conn, msg string) error {
    mlen := fmt.Sprintf("%04d", len(msg))

    _, err := conn.Write([]byte(mlen + msg))
    return err
}

The program will run 100000 goroutines at same time, and all goroutines will send message to the same connection。 I am doubt that server will receive error message like "hellohelloworldworld", but there is no problem when the program run in my Ubuntu 14.04LTS.

So, Do multiple goroutine will invoke a method on a Conn simultaneously?

=========================================================================

How can I keep the Write method atomic?

  • 写回答

1条回答 默认 最新

  • dongtangu8403 2016-06-20 12:47
    关注

    The documentation states:

    Multiple goroutines may invoke methods on a Conn simultaneously.

    There is no mention of whether each individual write is atomic. While the current implementation may ensure that each call to Write happens completely before the next call can begin, there is no guarantee in the language specification.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂