4

In Objective-C, under what scenarios/use case do you declare atomicity for your properties in your iOS/cocoa development? Please list practical use cases.

Note: I understand the difference between atomic and non-atomic, but there have been few who have answered this in the context of: "I have used atomic property when I am doing this and it's absolutely needed". Most answers on atomic/non-atomic have been theoretical and superficial.

Boon
  • 37,606
  • 51
  • 186
  • 296
  • Poll questions are off topic here. – cHao Nov 21 '13 at 13:44
  • possible duplicate of [Atomic vs nonatomic properties](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties) – David Snabel-Caunt Nov 21 '13 at 13:50
  • 1
    I don't believe this is a poll, rather a question about when to use atomic or nonatomic. – David Snabel-Caunt Nov 21 '13 at 13:51
  • 1
    Yes, it's a question on when to use atomic. I have not seen a strong case so far in day to day operation. – Boon Nov 21 '13 at 13:54
  • 1
    This question is not off-topic, nor is it opinion-based. What has SO become lately? – Boon Nov 21 '13 at 13:57
  • IMHO the Objective-C tag does attract a lot of low quality questions. Someone with your rep and badges should be given the benefit of the doubt though! – David Snabel-Caunt Nov 21 '13 at 14:05
  • 2
    Opinion based? Since when is the reason to require memory barriers and synchronization primitives an *opinion*? With all respect to the OT flaggers: if you have no idea about the problem, then just don't touch the "flag" button. – CouchDeveloper Nov 21 '13 at 14:46
  • The quality of the site is definitely going downhill with the excessive abuse of flagging on SO posts. – Boon Nov 21 '13 at 17:59
  • @CouchDeveloper: Since atomicity is the default. Since you could *always -- or never --* declare your properties atomic, without stuff breaking. And more importantly, since both (and anything else!) are perfectly valid answers to the question -- which isn't even asking about atomicity itself (which the OP allegedly already understands), but very specifically and literally asks people when they use it, and literally says "Please list practical use cases". This is the very definition of "primarily opinion-based"; the only invalid/incorrect answer would be if someone's lying. – cHao Nov 22 '13 at 22:37
  • @cHao The "atomic" attribute is a merely a simple synchronization primitive, similar like a mutex. Now, we all can imagine that we can implement a "critical section" with a mutex, and we known what a "critical section" is. But now you are stating, the usage -- respectively the non-usage -- of a "critical section" is primarily opinion based. – CouchDeveloper Nov 22 '13 at 23:12
  • It is unclear how lack of atomic access enforcement can cause a concurrency problem, given a simple object pointer. (This is assuming that even the nonatomic logic uses "safe" code for doing retain/release.) – Hot Licks Nov 23 '13 at 13:34
  • @CouchDeveloper: No, i'm saying that **this question** is primarily opinion-based. It's not asking about critical sections, synchronization primitives, `atomic` itself, or any other fact. It is specifically and literally asking for other people's assessments (ie: *opinions*) of when they see it necessary to declare their properties atomic. – cHao Nov 23 '13 at 14:54
  • 1
    @cHao It's not an *assessment* which determines whether you need to use the atomic attribute, even less assessment's of certain *people*. It's the *scenario* and the given circumstances and requirements which determine whether you need to use atomicity in order to get a reliable implementation. A valid answer has nothing to do with your or mine opinion. – CouchDeveloper Nov 23 '13 at 15:05
  • @CouchDeveloper: Really? "I always declare my properties atomic" is a valid answer. So is "I *never* do", and so is everything in between. There literally is no invalid answer, as long as they say when they use it, are telling the truth, and don't provide code that won't compile. – cHao Nov 23 '13 at 15:11
  • 1
    @cHao The OP specifically asked for *scenarios*. ;) So, valid answer would be: "Given this scenario, atomic is *required*, otherwise there is a potential race condition, albeit the probability is 0.001%") – CouchDeveloper Nov 23 '13 at 15:18
  • @CouchDeveloper: And how many of those scenarios are there? An infinite number. And "always" and "never" are still valid, even without explanation. This is a poll, plain and simple. – cHao Nov 23 '13 at 15:22
  • CouchDeveloper is correct. Just because there are different answers doesn't make this an opinion based questions. The practical scenarios are fact-based but different people may have use case info others aren't aware of. – Boon Nov 24 '13 at 14:50

2 Answers2

3

By default @property makes your property atomic. you specify nonatomic when you don't want it. Atomicity is useful when you have two object that might modify the same object at the same moment.

Nicolas Manzini
  • 7,809
  • 6
  • 58
  • 77
1

Put in a simple way:

If there are a risk that two threads can get/set the same property at the same time, then you need to use atomic. The atomic keyword prohibits a property to be changed when another thread is getting it.

The default value atomic is slower then nonatomic. That's why you most often use nonatomic

Mikael
  • 3,476
  • 1
  • 26
  • 42
  • So if we have a property that will be set in another thread, while its value will be obtained in main thread, should the property be atomic? Or is such consideration irrelevant for most applications? – Boon Nov 21 '13 at 14:00