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

sync/atomic: atomic.Value doesn't support CompareAndSwap #11260

Closed
OneOfOne opened this issue Jun 17, 2015 · 11 comments
Closed

sync/atomic: atomic.Value doesn't support CompareAndSwap #11260

OneOfOne opened this issue Jun 17, 2015 · 11 comments
Milestone

Comments

@OneOfOne
Copy link
Contributor

atomic.Value should have CompareAndSwap support.

If there's interest, I'll clean up my patch and put it up for review.

@josharian
Copy link
Contributor

/cc @dvyukov

@cespare
Copy link
Contributor

cespare commented Jun 17, 2015

What if you store uncomparable types in a Value (maps, slices, funcs, ...)?

@dvyukov
Copy link
Member

dvyukov commented Jun 17, 2015

Mutex usually works well for protecting write side of Value.

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jun 17, 2015
@OneOfOne
Copy link
Contributor Author

@cespare that's a good point, I was thinking about pointers mostly, didn't think about storing values.

@dvyukov I was trying to avoid that, and it seemed logical to have CAS anyway since most languages that have atomic.Value-style built-in already supports CAS. (ex: c++ java)

@dvyukov
Copy link
Member

dvyukov commented Jun 17, 2015

@OneOfOne If the only argument is that other languages have it, then I propose to not implement it.

@OneOfOne
Copy link
Contributor Author

@dvyukov not at all, I was just giving an example.

A good usage case for it is a (doubly) linked list, and avoiding mutexes all together.

@dvyukov
Copy link
Member

dvyukov commented Jun 17, 2015

Lock-free doubly-linked lists are insanely difficult to implement and are very slow because (1) link-based data structures are slow due to indirection and (2) shared data structures with high mutation rate are slow due to cache contention. And they can be implemented with atomic.CompareAndSwapPointer.
This does not look like a very convincing use case for addition of the new API.

@OneOfOne
Copy link
Contributor Author

I understand that, but atomic.Value is there and it exists, it has Load/Store like the other types in the atomic package, it's missing a function.

It's not an addition of a new API, it's adding a missing functionality.

@adg
Copy link
Contributor

adg commented Jun 17, 2015

I think Dmitry is referring to the addition of this new function as "the new API".

I don't see how the new API would work with non-comparable values. It seems to invite misuse.

To risk repetition, if your use case is pointers then you should use atomic.CompareAndSwapPointer.

@dvyukov
Copy link
Member

dvyukov commented Jun 18, 2015

Value.Load/Store are different from CompareAndSwap/Add/Exchange:

  1. They support a very well-defined use case (see examples), which is very efficient and relatively simple to code, and the one we want to promote for cases where high performance and scalability are required.
  2. They are supported on all types as opposed to CompareAndSwap/Add.

@rsc
Copy link
Contributor

rsc commented Oct 16, 2015

No thanks, per conversation above.

@rsc rsc closed this as completed Oct 16, 2015
@golang golang locked and limited conversation to collaborators Oct 17, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants