It’s worth noting that not only do you need to synchronize access to the singleton, you need to synchronize access to the singleton’s state as well. And even if you manage that at a fine-grained layer, you’re still setting yourself up for all the problems associated with singletons: http://c2.com/cgi/wiki?SingletonsAreEvil . If you have a bunch of immutable state, then build unexported package variables in the packag…
Singleton Pattern in Go
21–30 of 76 posts
Re: Singleton Pattern in Go
#22I thought there was a lot of literature pretty convincingly arguing that singleton is in fact an anti-pattern.
Letting DI frameworks handle your singletons is the best way to get the best of both worlds.
Re: Singleton Pattern in Go
#23It’s worth noting that not only do you need to synchronize access to the singleton, you need to synchronize access to the singleton’s state as well. And even if you manage that at a fine-grained layer, you’re still setting yourself up for all the problems associated with singletons: http://c2.com/cgi/wiki?SingletonsAreEvil . If you have a bunch of immutable state, then build unexported package variables in the packag…
Do you think it would be fine to use a singleton if the writes are all happening in a single thread and it's not critical that the reads be synchronized? Sharing state across go routines seems like something to be avoided. In vanilla Java it's always not easy to avoid sharing state.
The code you write is either thread-safe, used in a single-threaded context, or a pinless grenade.
Re: Singleton Pattern in Go
#24Singletons are really just as bad as global variables. Why? Because they are global variables.
Re: Singleton Pattern in Go
#25This post is chock-full of antipatterns. Both the singleton pattern and double checked locking should be avoided.
Yea it's easy enough to use dependency injection to satisfy these supposedly "global" requirements. About the only global state I can tolerate is read-only constants, but even then you are much better off with DI just because of testing.
Re: Singleton Pattern in Go
#26Your "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.
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.
(Asking because this pattern is used by libcxxabi code)
Re: Singleton Pattern in Go
#27This post is chock-full of antipatterns. Both the singleton pattern and double checked locking should be avoided.
Yea it's easy enough to use dependency injection to satisfy these supposedly "global" requirements. About the only global state I can tolerate is read-only constants, but even then you are much better off with DI just because of testing.
Re: Singleton Pattern in Go
#28This is cute, but I think (someone will correct me if I'm wrong) that the real idiom is: things that would be singletons in Java are instead servers managing a channel in Golang.
When passing singletons around, you are communicating with shared state. Sometimes, there is no way around it.
Re: Singleton Pattern in Go
#29It’s worth noting that not only do you need to synchronize access to the singleton, you need to synchronize access to the singleton’s state as well. And even if you manage that at a fine-grained layer, you’re still setting yourself up for all the problems associated with singletons: http://c2.com/cgi/wiki?SingletonsAreEvil . If you have a bunch of immutable state, then build unexported package variables in the packag…
Do you think it would be fine to use a singleton if the writes are all happening in a single thread and it's not critical that the reads be synchronized? Sharing state across go routines seems like something to be avoided. In vanilla Java it's always not easy to avoid sharing state.
Re: Singleton Pattern in Go
#30Earlier quoted context omitted.
Yea it's easy enough to use dependency injection to satisfy these supposedly "global" requirements. About the only global state I can tolerate is read-only constants, but even then you are much better off with DI just because of testing.
DI is an antipattern.