Re: Singleton instances and multithreading
Re: Singleton instances and multithreading
- Subject: Re: Singleton instances and multithreading
- From: Jean-François Veillette <email@hidden>
- Date: Thu, 12 Jul 2001 20:10:53 -0400
This exact code will work in single-threaded mode, just leave mylock to
null.
Once you know you are going multi-thread, you could post a notification,
initialize mylock to an instance of NSLock, and you are done. I'm not
sure, but IIRC there is already a notification posted for that purpose.
Once you know you are done with multi-thread, you may clear mylock and
set it to null.
A message to null is a no-op.
- jfv
I think you could put some stuff in there to check the
multithreaded-ness of the program and skip the locking if it's single
threaded.
+sharedInstance
{
[mylock lock];
do stuff to check/create singleton
[mylock unlock];
return singleton instance
}