Re: Synchronizing Thread Execution
Re: Synchronizing Thread Execution
- Subject: Re: Synchronizing Thread Execution
- From: glenn andreas <email@hidden>
- Date: Tue, 5 Dec 2006 09:08:09 -0600
On Dec 4, 2006, at 7:04 PM, Scott Ribe wrote:
// single static variable shared by all instances:
static id foo = nil;
Not good. Whether it uses some methods of NSObject and fails right
away, or
whether it just uses the pointer to access a lock via hash and
fails after
you try this with a second variable in some other class. But this
should be
fine:
static id foo = [[NSObject alloc] init];
This has two problems - one, it won't compile in plain Objective-C
because initializer elements need to be constant (and requires you to
use Objective-C++ to compile)
This, however, also has the problem of calling the Objective-C
runtime during global initialization (i.e., before main()). This
probably doesn't cause much of a problem with NSObject, but in
general other classes won't be happy (and if somebody else sees this
in the codebase and don't realize it, they might thing "static
variables initialized with Objective-C" is a good idea leading to a
frustrating "why does this work but this nearly identical thing
doesn't" debugging session).
A safer alternative would be use:
static NSString *foo = @"com.foo.myapp.myfile.foo";
Note that you'd want to make sure that this string is unique within
your code (since if you use this technique multiple time with the
same value, they can end up with the same address and this defeats
the goal of being "unique"), and thus the whole "reverse DNS" style
value.
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | flame : flame fractals & strange attractors : build,
mutate, evolve, animate
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden