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

map不能用cas更新value #42

Closed
flippedme opened this issue Sep 28, 2020 · 5 comments · Fixed by #43
Closed

map不能用cas更新value #42

flippedme opened this issue Sep 28, 2020 · 5 comments · Fixed by #43
Labels
bug Something isn't working

Comments

@flippedme
Copy link
Contributor

实际描述

  • 文件路径:under-the-hood/gosrc/sync/map.go
  • 原文段落:L69
// 一个 entry 可以被原子替换为 nil 来删除:当 m.dirty 是下一个创建的,它会自动将 nil 替换为 expunged 且
// 让 m.dirty[key] 成为未设置的状态。

预期描述

// 一个 entry的p字段 可以被原子替换为 nil 来删除:当 m.dirty 是下一个创建的,它会自动将 nil 替换为 expunged 且
// 让 m.dirty[key] 成为未设置的状态。
@changkun
Copy link
Member

PR welcome

@changkun changkun added the bug Something isn't working label Sep 28, 2020
@flippedme
Copy link
Contributor Author

@changkun Is there a way to get the pointer of a value stored in the map, and use CAS to change that? suddenly curious

@changkun
Copy link
Member

@flippedme I don't fully understand your question. Could you please add an example that explains more of your curiosity?

@flippedme
Copy link
Contributor Author

@changkun Well, for example in an object

type People struct{ age int32 }
p := People{}
atomic.CompareAndSwapInt32(&p.age, 30, 35)

and I wondered wether we can do the same thing to a map, you know, get-the-address-cas-change pattern.
But I figured that the address is not always valid because of map growth or shrink.
thanks

@changkun
Copy link
Member

@flippedme Aha. Indeed. Sadly, you can't. map access is not concurrent safe, this is well known.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants