--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Is there really any difference between the locks and the store/load functions?
They both deviate from the go mantra about sharing through communication and while the locks may be easier to handle they serve the same purpose.
If these types and functions are not really unsafe then they belong the atomic package fully.
What harm can it cause? Most other languages of similar kind have such utilities so it can't be all bad.
/Henrik
Ah yes indeed. I was more getting at the general principle than the runtime characteristics. They are both used to provide a consistent view of shared data.
I think this sort of performance improvement is a natural part of a runtime/library evolution and many core systems are getting more common.
I am not sure but I remember a Niagara with some 32 cores and that was some time ago. Surely systems come with even more cores pretty cheap today.
Use locks and channels. Don't be clever.For thoroughness, the rest of this document explains things in moredetail, but if you need to read this document to decide if yourprogram is correct, you are being clever.
But that principle must not be carried to an absurdity as well, right?We already use sync/atomic in regexp, net and in some other packages
for a reason.
The problem with scalability is that you can not get away with *any*
amount of simple code. Sometimes you just need an atomic.
> We need to provide ways for end programmers to avoid being clever. MakingWhat is required here is something where you can put an object to, and
> atomic operations polymorphic does the opposite: it encourages cleverness.
retrieve the current version of the object. We can call it some other
way, but this is an atomic pointer as an abstraction.
// An Interface provides an atomic load and store of a consistently typed value.
// Interfaces can be created as part of other data structures.
// The zero value for an Interface returns nil from Load.
// Once Store has been called, an Interface must not be copied.
type Interface struct {v interface{}}// Load returns the value set by the most recent Store.// It returns nil if there has been no call to Store.func (p *Interface) Load() interface{}// Store sets the value of the interface to v.// All calls to Store for a given Interface must use values of the same type.// Store of an inconsistent type panics, as does Store(nil).func (p *Interface) Store(v interface{})
Load:typ = atomicload(&p.v.typ)if typ == nil { return nil }val = atomicload(&p.v.val)return {typ, val}Store:if v == nil { panic }for {typ = atomicload(&p.v.typ)if typ == nil && !atomiccas(&p.v.typ, nil, v.typ) { continue }if typ != v.typ { panic }break}atomicstore(&p.v.val, v.val)
> We need to provide ways for end programmers to avoid being clever. Making
> atomic operations polymorphic does the opposite: it encourages cleverness.
What is required here is something where you can put an object to, and
retrieve the current version of the object. We can call it some other
way, but this is an atomic pointer as an abstraction.
Looks good to me.
I think I even wrote something along these lines some time ago... right:
https://codereview.appspot.com/5554065/diff/3/src/pkg/sync/atomic/ifaceptr.go
It was an atomic interface that can change types dynamically
(double-word atomic loads are based on a lock-free seq-lock).
I agree that we don't need to change types for the copy-on-write case.
We can even require that the atomic.Interface is initialized to the
final type before other loads and stores:
var x atomic.Interface = atomic.MakeInterface(new map[string]string)
It will make Load/Store impl simpler and usage even stricter.
So, just so I get this straight. This value serves the only purpose to allow a proper read and write across threads/goroutines? It will not provide compare and swap? I kind of, perhaps naively, thought that that was the intended use case.