views:

190

answers:

1

I'm still getting the hang of working in a threaded environment using NSOperation, and I'm interested in understand if using the 'nonatomic' property is even worthwhile if you're subclassing NSOperation?

If your defaultQueue is set to 1, is the additional overhead of nonatomic worth it? Might it be needed if you're executing more than 1 thread per operation queue?

Thanks for providing a solution on understanding this property in a threaded environment better.

+2  A: 

If you have an object (or a property of that object) that you can guarantee it not accessed from multiple threads at the same time, then you can avoid potential overhead of atomic access to that property by using nonatomic. In the simplest case of creating an operation and enqueuing it on an NSOperationQueue, each NSOperation instance will either be getting set up in the invoking thread or running in a single background thread; so at any one time, only a single thread should be accessing it.

The more general question of when nonatomic is useful depends in part on whether you are running with garbage collection enabled. According to the Objective-C release notes:

Access to properties is atomic by default. This is trivial under GC for almost everything and also trivial under non-GC for everything but objects and structures. In particular atomic access to retained objects under non-GC conditions can be expensive. As such, a nonatomic property attribute is available.

tjw