1

Does Grand Central Dispatch care about atomic and nonatomic keywords, or we have to specify atomic anyway?

I know that atomic and nonatomic keywords in property declarations will generate different setters and getters, atomic will be thread safe.

Whitecat
  • 3,882
  • 7
  • 48
  • 78
Lexorus
  • 199
  • 7
  • What is Grand Central Dispatch? You should add tag so you can be more clear about that. – Whitecat Jun 12 '15 at 17:55
  • 1
    @Whitecat I thought that iOS and Objective-C tags will be enough, but thanks for comment I will edit. – Lexorus Jun 12 '15 at 18:09
  • You should post your question as the first sentence of the post. Then post what you know. Using words like "this" and "it" are confusing. – Whitecat Jun 12 '15 at 18:16

1 Answers1

3

Using atomic is one way to synchronize a property being used from multiple threads. But there are many mechanisms for synchronizing access from multiple threads, and atomic is one with fairly limited utility. I'd suggest you refer to the Synchronization chapter of the Threading Programming Guide for a fuller discussion of alternatives (and even that fails to discuss other contemporary patterns such as GCD serial queues and reader-writer pattern with a custom, concurrent queue).

Bottom line, atomic is, by itself, neither necessary nor sufficient to ensure thread safety. In general, it has some limited utility when dealing with some simple, fundamental data type (Booleans, NSInteger) but is inadequate when dealing with more complicated logic or when dealing with mutable objects.

In short, do not assume that you should use atomic whenever you use GCD. In fact, if you use GCD, that generally obviates the need for atomic, which, in fact, will unnecessary and adversely impact performance in conjunction with GCD. So, if you have some property being accessed from multiple threads, you should synchronize it, but the choice of which synchronization technique to employ is a function of the the specific details of the particular situation, and GCD often is a more performant and more complete solution.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • In [this](https://stackoverflow.com/questions/29093312/is-this-gcd-implemented-getter-setter-thread-safe-and-work-better-than-synchron), the answer says atomic would outperform gcd, are there any statistics supporting that? – gabbler Mar 21 '18 at 09:24
  • That compares GCD to `@synchronized`, which is different. But it's easily benchmarked, e.g. https://stackoverflow.com/a/20939025/1271826. But, the speed difference between atomic and GCD is not generally not material, not observable. So, atomic can outperform GCD (tho not enough to see it in real apps), but more importantly, in Obj-C it's simpler to use. The GCD vs atomic issue is generally not speed, though. The reality is that in 99.9% of the situations we developers deal with, `atomic` is structurally insufficient to provide the necessary synchronization. It generally just can't do the job. – Rob Mar 21 '18 at 16:56
  • Bottom line, I use Objective-C `atomic` whenever I can, which is practically never. – Rob Mar 21 '18 at 16:59
  • I changed a NSString property from 'atomic' to 'nonatomic', crash rate has increased, last backtrace is `-[NSString doubleValue],_NSScanDoubleFromString, _objc_msgSend`, while getting the value of this property, some other thread is also setting it, I prefer not to use atomic, it is just convenient to use, teammate like to use it though. – gabbler Mar 22 '18 at 06:35
  • 1
    If you’re doing anything with this while changing it on another thread, you absolutely must synchronize it. The only question is whether atomic is sufficient or not. It protects you in the “read from one thread and set in another” scenario, but if you (or some future programmer) ever, for example, used a mutable string, atomic wouldn’t be adequate. By the way, if you get problems like this, make sure to debug your app with the thread sanitizer. See https://developer.apple.com/videos/play/wwdc2016/412/. – Rob Mar 22 '18 at 08:45