0

I am messed with a concept of thread safe and non-thread-safe concept for atomic and nonatomic property attributes, whose explanation is:-

atomic means thread safe and nonatomic means non-thread-safe and nonatomic gives high performance.

@property (nonatomic, weak) NSArray *myArray;

But what exactly thread-safe and non-thread-safe means ? Please explain in very simple way !

shubham mishra
  • 971
  • 1
  • 13
  • 32
  • Where is the explanation of thread-safe and non-thread-safe in that page ? – shubham mishra Jun 10 '16 at 07:33
  • And what about the difference between the two – shubham mishra Jun 10 '16 at 07:55
  • I am not asking about atomic or non-atomic. I am asking :- what exactly thread-safe and non-thread-safe means and their difference? Please explain in very simple way ! – shubham mishra Jun 10 '16 at 08:01
  • 1
    Chill man, got it ! – shubham mishra Jun 10 '16 at 08:42
  • 1
    `atomic` absolutely does not mean "thread safe". Be careful about making declarations like this when you are not sure what things mean(to minimize spread of misinformation). `atomic` simply refers to synchronization just around that property, it does not make your program "thread safe". – Kekoa Jun 11 '16 at 01:59

2 Answers2

0

Both are non Thread safe.

Atomic is only a read-write safe.

Read more about here:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
0

First of all, we should know what is thread. All the iPhones are using multi core processors. Usually, the whole app is running in the main thread. (i.e. running in the single core).

The developers should take advantage of the multi core processors and creating separate threads to run different processes at the same time in different core. Thereby, we increase the efficiency of the app.

Suppose, you are using a string with nonatomic property and you are using two threads in your app. when the two threads are trying to change/access the string at the same time, the result will be unpredictable. because we don't know which process will run at which time.

So, at that time, we have to set the string with property atomic. so that one process/thread will handle the string at a time. Like that, we are making it thread safe.