We have a static (singleton) class which will be used in a mutithreaded environment. We use mutex in its constructor and other mrmber functions. However there is no mutex for the destructor. Destructor do some tasks like cleaning up of some other member objetcs etc.
Do we need to a mutex in the distructor also ?
...
So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this:
class singleton {
private:
singleton(); // private constructor so users must call instance()
...
I've always assumed that DbNull.value was a singleton. And thus you could do things like this:
VB.NET:
If someObject Is DbNull.Value Then
...
End if
C#:
If (someObject == DbNull.Value)
{
...
}
But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison o...
I am trying to implement the Jon Skeet's example
public sealed class Singleton
{
Singleton()
{
}
public static Singleton Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
...
I was thinking about the classic issue of lazy singleton initialization - the whole matter of the inefficiency of:
if (instance == null)
{
instance = new Foo();
}
return instance;
Anyone who knows what a Singleton is is familiar with the issue(you only need the if once). It's trivial but irritating.
So, I thought of an alternate ...
I am using the singleton pattern in a wpf app, but having doubts about how to make it work with multiple threads.
I have a class called Monitor which maintains a list of "settings" to watch, for different "devices". Outline shown below.
On my main thread I am doing
Monitor.getMonitor.register(watchlist) or Monitor.getMonitor.unregist...
Before anyone flips out on me about singletons, I will say that in this instance it makes sense for me to have a singleton given the wide use of this object throughout my code and if someone has a better way short of DI I would like to hear but I would hope that this would not be the focus of this post, moreso helping solve it would be.
...
I am using mvc.net with StructureMap to scan and register all repositories and services for me. Now I want to register and cache by Singleton. How can I do? Thanks...
IContainer container = new Container(x => {
// Register Repositories and Services
x.Scan(y => {
y.AssemblyContainingType<SomeRepos...
I have had several occasions recently to access a specific class several times over a relatively small time frame.
So I've been storing the value of the class in Session and trying to access it on page load, if it's not available creating a new instance and storing that in session.
So instead of constantly replicating the same code for...
Gang of four uses a load balancer example to demonstrate the singleton pattern. I'm just wondering why a singleton needs to be used in that example? Are there any other real examples that specifically demonstrate using a singleton pattern?
I just want to know more specific reasons why I would want to prevent the creation of more than o...
I have recently started doing some ActionScript/Flex programming and I am... Surprised... By the number of singletons I see. They are everywhere! The standard library, the frameworks... Heck, I even read a blog post this morning where the author mentioned that he refactored some of his classes INTO singletons!
Is there any reasonable ex...
Hey All,
I'm implementing a singleton class as follows:
static Singleton* _singletonInstance;
@implementation Singleton
+(void)initialize
{
_singletonInstance = [[Singleton alloc] init];
}
+(Singleton*)instance
{
return(_singletonInstance);
}
initialize only gets called the first time someone calls instance. I then have ...
I know what a singleton is, but while walking through a web-app, my co-worker said "singleton-modeless". What does he mean by this?
...
It's understandable that many design patterns can in some cases be abused, and like mom always said: "Too much of a good thing isn't always good!"
I'm noticing that these days, I'm using Singletons a lot, and I'm worried that I might be abusing the design pattern myself, and running deeper and deeper into a bad-practice kind of habit.
...
Hi all,
I have a commercial application made with C,C++/Qt on Linux platform. The app collects data from different sensors and displays them on GUI. Each of the protocol for interfacing with sensors is implemented using singleton pattern and threads from Qt QThreads class. All the protocols except one work fine. Each protocol's run func...
I'm creating a game that uses cards.
I have an AppController class with one instance in the nib.
The AppController instance has an NSArray instance variable called wordList.
On init, the nib's instance of AppController generates a new GameCard.
Every gamecard has an array of words containing 5 words selected at random from the the list i...
The idea is that the ApplicationSettings class will get some default values from a configuration / resource file and later on some but not all from those settings will be applied to UserSettings
...
I am creating a custom .net hardware framework that will be used by other programmers to control some hardware. They will add a reference to our DLL to get to our hardware framework. I am in need of a shared class that will be accessed from multiple applications (processes).
The singleton pattern seems to be what I need but it only wor...
Hi all,
I am using a singleton backbone in my application to handle accuring errors. They will be handled inside the singleton and broadcast a notification throughout the app when the error has been fixed. Anyways this is not what my question is about but when I pass a new error to the singleton object like this
[[SingletonErrors share...
I was reading the Singleton article on Wikipedia and I came across this example:
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {}
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHold...