Golang的bufio包中的goroutine是安全的吗?

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

Is golang bufio goroutine safety

问题

多个goroutine可以同时调用bufio的Read函数吗?我读了bufio的源代码,看起来它没有适当的方法来保护缓冲区只能被一个goroutine读取。

英文:

could multiple goroutines invoke bufio Read function at same time. I read the source code of bufio, and looks like it doesn't have proper method to protect buffer would only read by one goroutine.

答案1

得分: 2

不,从缓冲区读取数据不是一个线程安全的操作。你需要进行协调管理。问题在于,从缓冲区读取数据会修改其状态,因此并没有合理的并发方式来进行读取操作。在读取结束后,需要移动一个位置标记,所以在第一个读取操作完成之前不能开始第二个读取操作。

英文:

No, reading from a buffer is not a thread safe operation. You have to manage coordination. Thing is, a read from the buffer modifies it's state there's not really any reasonable way to do it concurrently. There's a position marker that has to be moved at the end of the read so you can't begin a second read until the first completes.

huangapple
  • 本文由 发表于 2015年11月18日 10:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/33770691.html
匿名

发表评论

匿名网友

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

确定