Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top