4 ms·
Why do you need to synchronize class methods? Global state mutation?
by tomlu 12y ago
Why do you need to synchronize class methods? Global state mutation?
- chrisdevereux 12y agoLazily instantiating a shared object is the obvious reason. Doesn't need to be mutable.
- jonhohle 12y agodispatch_once(3)[0] can be used for lazy initialization without concern for the calling class and without synchronizing on a shared object. This is (likely) cheaper in most cases then entering a @synchronized block. 0 - https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/dispatch_once.3.html https://developer.apple.com/library/mac/documentation/Darwin...
- chrisdevereux 12y agoYes. In general, either GCD or lower level atomic ops are a better choice for thread-safety than @synchronized blocks. Just pointing out that there are reasons other than global mutable state for wanting a critical section in a class method.