Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Code:
@property (strong) NSURL * fileURL;

When would I not want to specify 'write atomically' or write 'non-atomically'?
 
"Atomic" means that the whole value of a variable will always be written or read in one go, so that other threads can not see a partially-written value.

Most modern CPU architectures guarantee atomicity anyway, for variables that are smaller or equal in size to a machine word. So it probably only changes the generated code for, say, double values on a 32-bit system.

Note that "atomic" is different from say, "volatile" in Java. It doesn't make any guarantees about the memory ordering of reads and writes.
 
"Atomic" means that the whole value of a variable will always be written or read in one go, so that other threads can not see a partially-written value.

Most modern CPU architectures guarantee atomicity anyway, for variables that are smaller or equal in size to a machine word. So it probably only changes the generated code for, say, double values on a 32-bit system.

Note that "atomic" is different from say, "volatile" in Java. It doesn't make any guarantees about the memory ordering of reads and writes.

Objective C is my first and only language at this point and I don't come from an extensive computer science background. I've only been coding for 8months.

Typically I write properties for objects that specify nonatomic, and I avoid atomic/nonatomic when primitive types.

I think I understand that nonatomic has something to do with checking something has written to disk properly, is that right?

But my original question is, when would I not specify either when creating an object property?

----------

good blog generally but this post touches on the subject.

okay_atomic

Ha, I like it.
 
I think I understand that nonatomic has something to do with checking something has written to disk properly, is that right?

But my original question is, when would I not specify either when creating an object property?

No, nothing to do with disk access.

atomic is only relevant if multiple threads are accessing your object simultaneously. 99.9% of time you don't have to worry about this, so "nonatomic" is almost always the right thing to do, because it can be faster.

(it was a mistake, IMO, that they made "atomic" the default in Objective C)
 
There's two atomics in Obj-C
1. Properties specified as nonatomic do not have extra code added to make them safe in a multi-thread environment. There's no keyword for atomic, it's simply the default case

2. A file write specified as atomic will write out to a temporary name first. Once all the data is transferred, it will overwrite the previous file in a single swoop so there's no chance of a partial write producing a garbage file.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.