Search Your Question

Difference between thread-safe and non-thread-safe in iOS

Ans : 

Thread-Unsafe -> If any object allow to modify by more than one thread at the same time.  (non-atomic property is thread-unsafe. Comments are welcomed)

Thread-safe -> If any object not allow to modify by more than one thread at the same time.Immutable objects are generally thread-safe. (atomic property attribute type. Comments are  welcomed)

In general, immutable classes like NSArray, let are thread-safe, while their mutable variants like NSMutableArray,var are thread-unsafe.

1 comment:

  1. 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.