[ios] What's the difference between the atomic and nonatomic attributes?

  • -Atomic means only one thread access the variable(static type).
  • -Atomic is thread safe.
  • -but it is slow in performance

How to declare:

As atomic is default so,

@property (retain) NSString *name;

AND in implementation file

self.name = @"sourov";

Suppose a task related to three properties are

 @property (retain) NSString *name;
 @property (retain) NSString *A;
 @property (retain) NSString *B;
 self.name = @"sourov";

All properties work parallelly (like asynchronously).

If you call "name" from thread A,

And

At the same time if you call

[self setName:@"Datta"]

from thread B,

Now If *name property is nonatomic then

  • It will return value "Datta" for A
  • It will return value "Datta" for B

Thats why non atomic is called thread unsafe But but it is fast in performance because of parallel execution

Now If *name property is atomic

  • It will ensure value "Sourov" for A
  • Then It will return value "Datta" for B

That's why atomic is called thread Safe and That's why it is called read-write safe

Such situation operation will perform serially. And Slow in performance

- Nonatomic means multiple thread access the variable(dynamic type).

- Nonatomic is thread unsafe.

- but it is fast in performance

-Nonatomic is NOT default behavior, we need to add nonatomic keyword in property attribute.

For In Swift Confirming that Swift properties are nonatomic in the ObjC sense. One reason is so you think about whether per-property atomicity is sufficient for your needs.

Reference: https://forums.developer.apple.com/thread/25642

Fro more info please visit the website http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to objective-c

Adding a UISegmentedControl to UITableView Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Warp \ bend effect on a UIView? Use NSInteger as array index Detect if the device is iPhone X Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3 ITSAppUsesNonExemptEncryption export compliance while internal testing? How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem? Change status bar text color to light in iOS 9 with Objective-C

Examples related to properties

Property 'value' does not exist on type 'EventTarget' How to read data from java properties file using Spring Boot Kotlin - Property initialization using "by lazy" vs. "lateinit" react-router - pass props to handler component Specifying trust store information in spring boot application.properties Can I update a component's props in React.js? Property getters and setters Error in Swift class: Property not initialized at super.init call java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US How to use BeanUtils.copyProperties?

Examples related to atomic

What exactly is std::atomic? What does "atomic" mean in programming? What is the difference between atomic / volatile / synchronized? Practical uses for AtomicInteger What's the difference between the atomic and nonatomic attributes?

Examples related to nonatomic

What's the difference between the atomic and nonatomic attributes?