Re: Singleton instances and multithreading
Re: Singleton instances and multithreading
- Subject: Re: Singleton instances and multithreading
- From: Christopher Lloyd <email@hidden>
- Date: Thu, 12 Jul 2001 12:38:17 -0400
I think it makes the most sense to have a singleton instance per thread
whenever possible, you can store it in the -threadDictionary, so your class
method looks something like..
+sharedInstanced {
NSMutableDictionary *threadDictionary=[[NSThread currentThread]
threadDictionary];
id shared;
if((shared=[threadDictionary objectForKey:@"MyClass"])==nil){
shared=[[[self alloc] init] autorelease];
[threadDictionary setObject:shared forKey:@"MyClass"];
}
return shared;
}
I believe Foundation does this for many of the classes (NSRunLoop,
NSNotificationCenter, NSNotificationQueue and others) and makes the most sense.
This avoids a lot of locks...
At 12:33 AM 7/11/2001 -0400, you wrote:
Well, I figured I'd join in the current rash of multithreading questions
to clarify something I don't quite understand about multithreading: can I
safely call class methods from spawned threads?
Specifically, I want to call the +sharedInstance method on a class I've
developed for an application. Can I do that and then safely message the
returned object without using DO?