Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances are atomic properties useful?

Objective-C properties default to atomic, which ensures that accessors are atomic but doesn't ensure overall thread-safety (as per this question). My question is, aren't atomic properties redundant in most concurrency scenarios? For example:

Scenario 1: mutable properties

@interface ScaryMutableObject : NSObject {}

@property (atomic, readwrite) NSMutableArray *stuff;

@end

void doStuffWith(ScaryMutableObject *obj) {
    [_someLock lock];
    [obj.stuff addObject:something]; //the atomic getter is completely redundant and could hurt performance
    [_someLock unlock];
}

//or, alternatively
void doStuffWith(ScaryMutableObject *obj) {
    NSMutableArray *cachedStuff = obj.stuff; //the atomic getter isn't redundant
    [_someLock lock];
    [cachedStuff addObject:something]; //but is this any more performant than using a nonatomic accessor within the lock?
    [_someLock unlock];   
}

Scenario 2: immutable properties

I was thinking that maybe atomic properties would be useful for avoiding locks when working with immutable objects, but since immutable objects can point to mutable objects in Objective-C, this isn't really much help:

@interface SlightlySaferObject : NSObject {}

@property (atomic, readwrite) NSArray *stuff;

@end

void doStuffWith(SlightlySaferObject *obj) {
    [[obj.stuff objectAtIndex:0] mutateLikeCrazy];//not at all thread-safe without a lock
}

The only scenarios I can think of where it's safe to use atomic accessors without a lock (and therefore worth using atomic properties at all) are:

  1. Working with properties that are primitives;
  2. Working with properties that are guaranteed to be immutable and not to point to mutable objects (such as an NSString or an NSArray of immutable objects).

Am I missing something? Are there any other good reasons to use atomic properties?

like image 650
shosti Avatar asked Jan 14 '11 21:01

shosti


People also ask

What is the importance why we study atoms and its properties?

By learning about atomic structure, we can find out how atoms combine and form many compunds. By learning about atomic structure, we can find out how atoms collide. By learning about atomic structure, we can find out why atoms do not have mass. By learning about atomic structure, we can find out how atoms disappear.

Why is atomic number an important property of an element?

The atomic number indicates the number of protons within the core of an atom. The atomic number is an important concept of chemistry and quantum mechanics. An element and its place within the periodic table are derived from this concept.

Why do we need atomic symbols?

The atomic symbols are useful in determining the number of protons, neutrons, and electrons. The atomic symbols are useful in identifying the components of a compound. The atomic symbols are useful in identifying the group and the period to which the element belongs.

What are the properties of atomic?

Atoms are indivisible particles, which cannot be created or destroyed in a chemical reaction. Atoms of a given element are identical in mass and chemical properties. Atoms of different elements have different masses and chemical properties. Atoms combine in the ratio of small whole numbers to form compounds.


1 Answers

You aren't missing anything; atomic's usefulness is largely limited only to situations where you need to access or set a particular value from multiple threads where that value is also integral.

Beyond a single value, atomic cannot be used for thread safety purposes.

I wrote quite a bit about it in a weblog post a while ago.

This question is also a [very well posed] duplicate of What's the difference between the atomic and nonatomic attributes?

like image 121
bbum Avatar answered Oct 10 '22 11:10

bbum