user5214391
user5214391

Reputation: 69

Which is thread Safe atomic or non-atomic?

After reading this answer I am very confused.

Some says atomic is thread safe and some are saying nonatomic is thread safe.

What is the exact answer of this.

Upvotes: 2

Views: 535

Answers (4)

Rob
Rob

Reputation: 437552

Obviously, nonatomic certainly isn’t thread safe. More interestingly, atomic is closer, but it is insufficient to achieve “thread safety”, as well. To quote Apple’s Programming with Objective-C: Encapsulating Data:

Note: Property atomicity is not synonymous with an object’s thread safety.

It goes on to provide an example:

Consider an XYZPerson object in which both a person’s first and last names are changed using atomic accessors from one thread. If another thread accesses both names at the same time, the atomic getter methods will return complete strings (without crashing), but there’s no guarantee that those values will be the right names relative to each other. If the first name is accessed before the change, but the last name is accessed after the change, you’ll end up with an inconsistent, mismatched pair of names.

This example is quite simple, but the problem of thread safety becomes much more complex when considered across a network of related objects. Thread safety is covered in more detail in Concurrency Programming Guide.

Also see bbum’s Objective-C: Atomic, properties, threading and/or custom setter/getter.

The reason this is so confusing is that, in fact, the atomic keyword does ensure that your access to that immediate reference is thread safe. Unfortunately, when dealing with objects, that’s rarely sufficient. First, you have no assurances that the property’s own internal properties are thread safe. Second, it doesn’t synchronize your app’s access to the object’s individual properties (such as Apple’s example above). So, atomic is almost always insufficient to achieve thread safety, so you generally have to employ some higher-level degree of synchronization. And if you provide that higher-level synchronization, adding atomicity to that mix is redundant and inefficient.

So, with objects, atomic rarely has any utility. It can be useful, though, when dealing primitive C data types (e.g. integers, booleans, floats). For example, you might have some boolean that might be updated in some other thread indicating whether that thread’s asynchronous task is completed. This is a perfect use case for atomic.

Otherwise, we generally reach for higher-level synchronization mechanisms for thread safety, such as GCD serial queues or reader-writer pattern (... or, less common nowadays, locks, the @synchronized directive, etc.).

Upvotes: 1

Sulthan
Sulthan

Reputation: 130102

Thread unsafety of operations is caused by the fact that an operation can be divided into several suboperations, for example:

a = a + 1

can be subdivided into operations

load value of a
add 1 to the loaded value
assign the calculated value to a.

The word "Atomic" comes from "atom" which comes from greek "atomos", which means "that which can't be split". For an operation it means that it is always performed as a whole, it is never performed one suboperation at a time. That's why it is thread safe.

TL;DR Atomic = thread safe.

Big warning: Having properties atomic does not mean that a whole function/class is thread safe. Atomic properties means only that operations with the given properties are thread safe.

Upvotes: 1

SWiggels
SWiggels

Reputation: 2296

As you can read at developer.apple you should use atomic functions for thread savety.

You can read more about atomic functions here: Atomic Man page

In short: Atomic ~ not splittable ~ not shared by threads

Upvotes: 0

Shamas S
Shamas S

Reputation: 7549

As is mentioned in several answers to the posted question, atomic is thread safe. This means that getter/setter working on any thread should finish first, before any other thread can perform getter/setter.

Upvotes: -1

Related Questions