Re: Maintaining *one* singleton over >1 thread
Re: Maintaining *one* singleton over >1 thread
- Subject: Re: Maintaining *one* singleton over >1 thread
- From: sinclair44 <email@hidden>
- Date: Tue, 18 Mar 2003 15:57:53 -0500
On 3/18/03 1:25 AM, "Jeff Disher" <email@hidden> wrote:
>
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.
Thanks! I tried it without the lock but moving my method-static variable to
file-static; nothing changed. But I then modified my object to how you
outline (and added extra lock/unlocks in other methods where needed) and my
NSLog() in -init only gets called once. There was another issue with
registering a timer in the non-main thread, but I resolved that myself.
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.