Reputation: 57
As stated in apples documents. I always thought that specifying a property as 'atomic' meant that it was thread safe, where only a single thread can set/access the property at a time. Whats the point of have atomic if its not thread safe and is alot slower. And if it is said to be not thread safe, what makes it considerably slower if you can still access it simultaneously?
Upvotes: 0
Views: 70
Reputation: 112857
Atomic mean that the individual property will be thread safe, that is an entire correct value will be operated on. But you may have many atomic objects and there may be several that are inter-related and more than one thread operation on them.
Suppose there is a first and last name. Each is atomic. no name will be partially changed. But thread 1 is changing the first and last while the other thread is accessing them. The first name is atomically changed, the other task accesses both the first and last, but they are not consistent with each other at tis point in time. The other thread changes the last name and they are now consistent. But the second thread has a mashup of first and second names.
Initial: Bob, Ace
Thread 1 accesses the first name: Bob
Thread 2 changes the first name to Carol
Thread 1 accesses the last name: Ace
Thread 2 changes the last name: Ventura
Thread has the name as Bob, Ventura
Thread has the name as Ace, Ventura
Each name, first and last were atomic properties.
If they were not atomic the first name might end up as Bce if the two threads swapped execution just after the first letter was changed.
Upvotes: 1