> In that case is it my responsibility to guard all access to my class' thread > unsafe ivars (like mutable objects for example) or is it the responsibility > of the user of my class to guard all access to my class' instances in his > code?
When dealing with Cocoa, the rule is that all classes must be assumed usable from the main thread only, unless they explicitly allow otherwise. In my own code, I adopted this rule too; if a class is thread-safe, I document it as such. If not, I may or may not mention it; if not, then it's assumed that that class must be used from the main thread only. I recommend adopting this rule too, and only making classes thread safe if it makes sense. While making every class you ever write thread-safe might be a good intellectual exercise, it's hard and time consuming, and I doubt there's many of us who do it. > With regards to this, should I care about thread safety at all? If you are > designing a class, even without any concurrency in it at all, where do you > decide to worry about thread safety in code outside your reach? You should never not care about thread safety! :) Of course that doesn't mean every method you write should be thread safe. Rather, you should make a conscious decision of whether a class is going to be thread-safe, write it as such, and document that fact in the header or elsewhere. If a user chooses to ignore it and use that class from 5 different threads, then the bug's in their code, not yours. _______________________________________________ Cocoa-dev mailing list ([email protected]) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
