Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-map #8

Open
francisLee777 opened this issue Jul 13, 2021 · 1 comment
Open

go-map #8

francisLee777 opened this issue Jul 13, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@francisLee777
Copy link

go-map 中可以加入并发访问时导致panic的情况

@SmartKeyerror SmartKeyerror added the enhancement New feature or request label Jul 13, 2021
@SmartKeyerror
Copy link
Owner

今天早上又翻了一遍 mapassign_faststr()mapaccess1_faststr()mapaccess2_faststr() 这几个函数的实现,对于是否存在并发访问的处理还是比较简单的,大概率不会在 XMind 里面补充了 😸 。

// 写 map
func mapassign_faststr(t *maptype, h *hmap, s string) unsafe.Pointer {

	// other codes ......

	// 判断当前是否有其它 Goroutine 正在写 hmap
	if h.flags&hashWriting != 0 {
		throw("concurrent map writes")
	}

	// other codes ......

	// Set hashWriting after calling t.hasher for consistency with mapassign.
	// 设置标志位
	h.flags ^= hashWriting

	// other codes ......

	// 如果此时 flags 中不包含 hashWriting 的话,那么必然并发写的问题
	if h.flags&hashWriting == 0 {
		throw("concurrent map writes")
	}

	// 恢复标志位
	h.flags &^= hashWriting
}



// value := map[key] 形式的读 map
func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {

	// other codes ......

	// 判断是否有其它 Goroutine 正在写 map
	if h.flags&hashWriting != 0 {
		throw("concurrent map read and map write")
	}

	// other codes ......
}

因为 hashmap 本身就并不是并发安全的,所以这块儿的代码处理也比较简单,感觉就是起一个提示的作用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants