views:

60

answers:

1

Looking at the definition for "initialize":

+ (void)initialize

Discussion

The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.) The runtime sends the initialize message to classes in a thread-safe manner. Superclasses receive this message before their subclasses.

it is stated that initialize is sent in a "thread-safe manner". Under the covers, is the runtime creating a lock to make that call thread safe or is it inherently thread-safe just from the way the runtime works? If it does lock, does that mean if you implement +initialize the runtime creates a lock it would not otherwise have created?

+1  A: 

I can't answer whether or not it locks, but regardless of whether or not you implement +initialize, it's still called. The default implementation may do something, but it's still called. So if the runtime does lock, then the lock is created regardless of whether or not the method is implemented by your subclass.

Jasarien
Exactly; and lock creation is really really cheap. Locks only become expensive when they are locked! Once class initialization is done the synchronization mechanism used therein is never used again. BTW: The source for the objective-c runtime is available; it is open sourced.
bbum
A piece of the puzzle then, thanks. I may have to fetch the runtime source and see just what happens then.
Kendall Helmstetter Gelner