Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for something to be thread safe in iOS?

I often come across the key terms "thread safe" and wonder what it means. For example, in Firebase or Realm, some objects are considered "Thread Safe". What exactly does it mean for something to be thread safe?

like image 534
Ali Mir Avatar asked Dec 30 '16 17:12

Ali Mir


2 Answers

Thread Unsafe - If any object is allowed to modify by more than one thread at the same time. Thread Safe - If any object is not allowed to modify by more than one thread at the same time.

Generally, immutable objects are thread-safe.

like image 141
Divesh singh Avatar answered Nov 09 '22 23:11

Divesh singh


An object is said to be thread safe if more than one thread can call methods or access the object's member data without any issues; an "issue" broadly being defined as being a departure from the behaviour when only accessed from only one thread.

For example an object that contains the code i = i + 1 for a regular integer i would not be thread safe, since two threads might encounter that statement and one thread might read the original value of i, increment it, then write back that single incremented value; all at the same time as another thread. In that way, i would be incremented only once, where it ought to have been incremented twice.

like image 35
Bathsheba Avatar answered Nov 09 '22 23:11

Bathsheba