英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论