I guess @synchronized blocks are not object dependent but thread dependent...right? In that case why do we pass self?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
@synchronized
is a construct provided by the language to create synchronized scopes. As it would be highly inefficient to use a simple global shared mutex, and thus serializing every single@synchronized
scope in the application, the language allows us to specify a synchronization point.Then it's up to the developer(s) to decide which synchronization points are appropriate for the task.
On an instance method, using self is common: the instance is the synchronization point. The
@synchronized(self)
scope can be called on any number of instances, but only once for a given instance. Every@synchronized(self)
scope will be serialized for a given instance.Of course, you are free to use another synchronization point if you want to do so. You can use the class (
@synchronized(self.class)
) or anything else that suits your needs.I question this practice, as it is a known anti-pattern in other languages. The crux of the issue is that someone else could also
synchronize
on your object, possibly causing deadlocks and other issues that would not have been present had you been using a private NSObject for the lock. For example:The object passed in is used to differentiate which
@synchronized
blocks correspond to locking each other. Usingself
is often convenient, but sometimes it’s a good idea to use some other object if you want to only synchronise smaller, more specific sections of code (eg. synchronise all access to a specificNSMutableDictionary
, rather than synchronising everything in the whole instance)I’m not sure what you mean by “thread dependent”. The purpose of
@synchronized
is for blocks of code that may be running on different threads, and you need to ensure only 1 runs at any time, without overlapping. Important for performing actions that aren’t thread-safe (like mutating collections, for example).