- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Edit Admin
That's got to work surely? I'll only get one in my program?
Admin
Obviously, the way to create singletons is to use
pthread_once
.Edit Admin
:%s/design pattern/bad programming/g
Edit Admin
Most (but not all) uses of the singleton pattern are bad programming. It is almost always better to manage the lifecycle of a single instance through more conventional methods- like just creating one instance of it.
Edit Admin
Whether typo or eggcorn, that's really poetic! The more conventional phrase would be "where oh where".
Edit Admin
What that will do is create a separate Frist instance for each compilation unit (.cpp file), not visible from other compilation units. To create a true program-wide singleton, you need to declare it as
extern
in the .h file, and have exactly one .cpp file defining the instance.Admin
Singletons are not bad if you don't code singularity into the class. Then they're just classes that your application happens to have one of.
What else are you going to do, create multiple resource pools and lose the benefit of pooling? Renegotiate a backend connection on each use? I've seen such things done in orgs that regarded application architecture as a nice-to-have-somewhere-else.
Admin
Pretty sure this is dual abuse. Abuse of singleton and abuse of OO aka classes. Was there any good reason why utils were class members instead of free-standing functions?
Admin
That code will get you one heap-allocated instance of Frist for every compilation unit that includes Frist.h. It is a bit of a pain to "properly" implement Singleton in C++ because initialization of global variables ("static initialization") before main() starts execution have no guarantees whatsoever. My aproximation for singletons in C++ is something like
class mySingleInstance { //Class internals, everything marked static, private constructors //... static bool isInitialized; static void initialize() { if (isInitialized) { return; } //actual initialization code } } //... bool mySingleInstance::isInitialized = false;
And then as near the top as possible in main() (or whatever is the very first bit of code that is guaranteed to need the class)
mySingleInstance::initialize();
(you could add mySingleInstance::initialize() to every class method for overly defensive coding with near assured side effects, or change the bool for a pointer to the class that has the actual implementations if you wish for everything to be heap allocated)
"Fun" fact: The OG Singleton pattern worked due to a quirk/implementation detail of Sun's JVM at the time. Said quirk is now part of the Java/JVM specification.
Admin
"g_Utility" kinda looks like "guilty", which nicely describes whoever wrote that code.
Admin
Besides the weird way to initialise (I rather have a global reference than a pointer). A linter that doesn't know to look for side effects of instancing a variable in C++ is utter garbage. Get rid of it.
Admin
Just use Meyer's singleton:
It is simple to write, lazy initialized and thread safe (instantiation, not use).
Edit Admin
To my knowledge .net is the only OO framework which guarantees that a static constructor is instanced only ONCE and BEFORE the type containing it is used which makes static member fields natural singletons.
Now in C++ (and other languages like Java) you don't have this luxury. In fact in C++ it isn't even defined when static variables are instances, so you have to do something like this instead:
Addendum 2025-08-12 05:06: And then out of habit I put a public in front of class for C++ ... ah, it really shows that I'm mostly writing C# these days.
Admin
"Stop being clever and don't try and apply a design pattern for the sake of saying you used a design pattern." Yes, this is the point: i spent the last 15 years reading other people code (something around a few millions line of code each year) and i still haven't found code where the developers don't fall in this trap; i think that certain approaches to design pattern are the biggest plague in modern IT, because they teach to stop thinking, and blindly apply solutions that have been developed for problems that have nothing to do with the system at hand.
Admin
In C++,
static
outside of any class/struct just makes the variable local to the file. In case of header, it means every file that includes it will get its own copy. Opposite of what you want for a singleton.It has a yet third unrelated meaning when used for a local variable in function, which basically makes variable "shared" between invocations of the function (which does happen to be a legit way to implement a singleton, i.e.
Singleton* getInstance() { static Singleton instance; return &instance; }
)