当尝试将此结构与多个 goroutine 一起使用时,有时我会遇到以下错误之一:
fatal error :并发映射读取和映射写入
或
并发映射写入
看完this thread我确保在构造函数中返回一个引用,并将一个引用传递给接收者。
使用它的完整代码在 this github repo 中
type concurrentStorage struct {
sync.Mutex
domain string
urls map[url.URL]bool
}
func newConcurrentStorage(d string) *concurrentStorage{
return &concurrentStorage{
domain: d,
urls: map[url.URL]bool{},
}
}
func (c *concurrentStorage) add(u url.URL) (bool) {
c.Lock()
defer c.Unlock()
if _, ok := c.urls[u]; ok{
return false
}
c.urls[u] = true
return true
}
最佳答案
在阅读您链接到的 Github 上的代码后,crawl()
函数接受一个 concurrentStorage
(不是指针)。
对于调用 crawl()
时的每个取消引用(即:*urlSet
),您都在复制 concurrentStorage
结构(包括sync.Mutex
) 而映射保留指向原始指针的指针。这意味着您的互斥量与每个 goroutine 是隔离的,同时它们共享相同的状态。
如果您将 crawl()
改为接受指针,并停止取消引用 concurrentStorage
,它将按您的预期工作。
关于go - 为什么即使有锁,GO 也会出现 'concurrent map writes' panic ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52797446/