Re: Maintaining *one* singleton over >1 thread
Re: Maintaining *one* singleton over >1 thread
- Subject: Re: Maintaining *one* singleton over >1 thread
- From: Jeff Disher <email@hidden>
- Date: Tue, 18 Mar 2003 01:25:14 -0500
Well, how are you holding the singleton instance? Is it a static in
the method or in the file? You _should_ be able to do this just be
creating a static instance of your object and a lock for it but not
declare it in any particular method (just make it a file-scoped
variable). Then, in the + (void)initialize method of the class, set
them to nil and register the class as the receiver of the
NSWillBecomeMultiThreadedNotification notification. In the method that
receives that notification, have it instantiate the lock. Then, in
your sharedInstance, work as before but use lock/unlock delimitation.
This _should_ work but I haven't tried it, myself. This will allow the
exactly one instance of the object and exactly one instance of a lock
to protect it. Plus, until your app becomes multi-threaded, you will
not incur any lock/unlock overhead since the lock on the object will be
nil (and messaging nil is always ok and takes no extra time).
I haven't tried this, though, so I may be missing the detail that makes
this totally bogus but, if you haven't tried it yet, give it a shot.
Hope that helps,
Jeff.
On Monday, March 17, 2003, at 08:34 PM, sinclair44 wrote:
Hello-
I'm writing a multithreaded app with a singleton that must be allocated
*exactly once*. I was doing it (when single-threaded) with
+sharedInstance
and a static variable. However, when the second thread calls
+sharedInstance, a new instance is allocated (which didn't happen
before)!
Is there an easy way to get a single instance of this singleton over
the
entire app?
(BTW, I'm aware of multithreading issues and the locks I need to
implement.)
Thanks,
sinclair44.
--
-- sinclair44
[self becomeWorldDictator];
- (void)becomeWorldDictator
{
[self coverLegalButt];
[[GeorgeBush principalClass] assassinate:[world currentLeaders]];
[[BinLaden principalClass] terrorize:[world citizens]];
[world setCurrentLeaders:[NSArray arrayWithObject:self]];
}
- (void)coverLegalButt
{
/* The above does not reflect any plans, expressed or implied, real or
imaginative, to kill or assassinate anyone, or to harm anyone in any
shape,
way or form. Any relation to actual events is purely coincidental. */
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
Jeff Disher
President and Lead Developer of Spectral Class
Spectral Class: Shedding Light on Innovation
http://www.spectralclass.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.