News for hackers. Stuff that matters.
Re:Singleton Pattern in Go by pcwalton on Sunday July 12, 2015 @12:42PM (#9873987)
Your "check-lock-check" code is probably broken (depending on the intricacies of Golang's memory model). If the compiler or CPU reorders any stores to the fields of "instance" after the assignment to "instance" itself, other threads could start working with a partially uninitialized object. Once() uses atomics on the fast path for a reason.
Re:Singleton Pattern in Go by skrap on Sunday July 12, 2015 @01:57PM (#9874277)
Totally! I was going to post the same thing. Double-checked locking is either impossible or really hard to get right, depending on the language and architecture's guarantees!

If you must use a singleton, I'd really recommend doing the so-called "aggressive" approach, which should have really been named the "actually won't crash sometimes" approach.

Re:Singleton Pattern in Go by jaytaylor on Sunday July 12, 2015 @03:39PM (#9874630)
+1 for the aggressive approach, mutexes are very fast in go.

I got curious and wrote a quick little benchmark test:

    $ cat bench_test.go
    package main
    
    import (
    	"sync"
    	"testing"
    )
    
    func BenchmarkMutex(b *testing.B) {
    	var m sync.Mutex
    	for n := 0; n < b.N; n++ {
    		m.Lock()
    		m.Unlock()
    	}
    }


    $ go test -v -bench=. bench_test.go
    BenchmarkMutex	50000000	        24.0 ns/op
    ok  	command-line-arguments	1.235s
A set of mutex.Lock() & .Unlock() calls takes only 24.0ns on average to complete.

Thus it's possible to lock/unlock more than 41 million times per second on the puny 2011 MacBook Air I used for this.

My .02c:

The post seems like a case of premature optimization.

Resource bottlenecks due to too much mutex locking in go does not seem like a case that will be commonly hit.

With infinite potential bottlenecks, I don't like to spend my time worrying and fussing over things that are:

A) Not yet a problem.

B) Unlikely to ever be a problem or give me grief.

Worrying about the overhead cost of locking falls squarely into just such a category.

Re:Singleton Pattern in Go by pcwalton on Sunday July 12, 2015 @03:49PM (#9874670)
Be careful with that benchmark. It's very vulnerable to SROA and constant propagation optimizing it away to nothing. (Doesn't look like Go's compiler optimizations are able to do that based on those numbers, but a modern optimizer will.)
Re:Singleton Pattern in Go by millstone on Sunday July 12, 2015 @03:47PM (#9874664)
Here's the mutex source: https://golang.org/src/sync/mutex.go . The fast path is just a CAS, which of course is going to be fast. But it's also important to know how it performs under contention.
Re:Singleton Pattern in Go by jsprogrammer on Sunday July 12, 2015 @03:49PM (#9874674)
This has no contention and is essentially a no-op? Or do I not understand go?
Re:Singleton Pattern in Go by pcwalton on Sunday July 12, 2015 @04:02PM (#9874726)
It is a no-op, but Golang's Plan 9-based compilers don't do much optimization, so they aren't able to eliminate it.
Re:Singleton Pattern in Go by AYBABTME on Sunday July 12, 2015 @09:26PM (#9875864)
Try again with many goroutines trying to lock/unlock in parallel (and GOMAXPROCS>1), the results are quite different!
Re:Singleton Pattern in Go by Manishearth on Sunday July 12, 2015 @02:41PM (#9874423)
On C++ if we're using a bool as the "check" and pthread_mutex_lock on a regular mutex would that work, or do we still need to hardcode fences?

(Asking because this pattern is used by libcxxabi code)

Re:Singleton Pattern in Go by maxlybbert on Monday July 13, 2015 @08:52PM (#9882199)
A few years ago, the C++ standard didn't guarantee that the double-checked idiom would work. Even if your threading library had some way to insert the necessary memory barriers, it was possible that your compiler would optimize important things away.

C++11 changes that. The language now has standard ways to add memory barriers, and compiler optimizations can't remove them.

Re:Singleton Pattern in Go by millstone on Sunday July 12, 2015 @03:38PM (#9874620)
Yes, this is broken on certain architectures like PPC. Another core may see the "check" bool as set, but the fields of the protected object as uninitialized. One way to address this is to insert a write barrier just before setting the check bool, and a read barrier just after reading it.
Re:Singleton Pattern in Go by hyperpape on Sunday July 12, 2015 @04:39PM (#9874879)
In modern Java, it's easy enough if you've been shown the pattern. What's hard is understanding/discovering it from first principles.
Re:Singleton Pattern in Go by hyperpape on Sunday July 12, 2015 @10:55PM (#9876117)
Someone apparently disliked this comment, but check out

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLo.... Scroll down to "Fixing Double-Checked Locking using Volatile." It's five lines of code without braces, and can be applied without any creativity or reasoning.

Re:Singleton Pattern in Go by cryon on Monday July 13, 2015 @04:28AM (#9876980)
You should use enums for singletons i Java. Simple to write, threadsafe and handles serialization. All with guarantees from the Java language specification.
Re:Singleton Pattern in Go by hyperpape on Monday July 13, 2015 @09:03AM (#9877817)
I wasn't actually talking about singletons, just the doublechecked locking pattern. It's also necessary for laziness on objects that are referenced by multiple threads.

I tend to think Singletons are a dubious design pattern, like a lot of other people.