Atomic and Non-Atomic

Discussion in 'iOS Programming' started by nashyo, Jul 31, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    Code:
    @property (strong) NSURL * fileURL;
    When would I not want to specify 'write atomically' or write 'non-atomically'?
     
  2. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #2
    "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.
     
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    good blog generally but this post touches on the subject.

    okay_atomic
     
  4. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #4
    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?

    ----------

    Ha, I like it.
     
  5. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #5
    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)
     
  6. solderguy1 macrumors member

    Joined:
    Apr 20, 2012
    #6
    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.
     

Share This Page