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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With