On Sep 3, 2013, at 3:52 AM, Jonathan Taylor <jonathan.tay...@glasgow.ac.uk> wrote:
> The complication is in ensuring this is threadsafe: ideally I would like to > make the copy on a thread other than the main thread. My understanding is > that properties themselves, even when designated atomic, are in some way not > fully threadsafe, although I haven't found a clear explanation of exactly > what makes them unsafe. This doesn’t have anything to do with atomic properties — all that keyword does is ensure that the property’s synthesized accessor methods are thread-safe with respect to the property value. It doesn’t help at all if you’re considering the object as a whole — you can access property a and then property b and get valid values for both, but they might not be consistent with each other because they were accessed at different times. If you want to look at the object as a whole you have to use some higher-level locking to keep it from being mutated while you work on it. > I don't know if the 'copy' method is threadsafe or not, I am inclined to > suspect not. No, you’re responsible for implementing -copy yourself. The base implementation in NSObject doesn’t copy any fields or properties implemented in subclasses. > Is there any way, then, that I can take a copy in a threadsafe manner? If the object is mutable, you have to synchronize every method that can alter its state, probably using @synchronized(self). This means you can’t use synthesized property setters. Then synchronize the copy method the same way. This is one reason why concurrent programming often relies on immutable objects — because they can be passed around multiple threads safely. Thread-safe access to mutable objects is a real pain and it’s very easy to create subtle bugs that only show up very rarely. —Jens
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com