Pankaj Bhardwaj
Pankaj Bhardwaj

Reputation: 2131

Atomic property and usage

I have read many stackoverflow answers for this like Is an atomic property thread safe?, When to use @atomic? or Atomic properties vs thread-safe in Objective-C but I have question for this:

Please correct me If I am wrong, This is like that I am using a count variable which I have declared with Atomic property and currently its value is 5 which is accessed by two thread , First thread that is increasing count value by 2 and 2nd thread decreasing the count value by 1 ,According to my understanding this go sequentially like when first thread increased its value which is now 5 + 2 = 7; after then only 2nd thread can access count variable and only decrease its value by 1 and that is 7 - 1 = 6?

Upvotes: 1

Views: 227

Answers (3)

user9358473
user9358473

Reputation:

Atomic based property where two threads are accessing same object to increase or decrease a value , You can understand this like both have accessed the same/whole value which is 5 but First thread trying to update this value then this is locked no other thread can update this at the same time however 2nd thread is also having same value which is 5 and can update only when Count object updation get exited by first thread.

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299275

First thread that is increasing count value by 2

That is not an atomic operation, and atomic in no way helps you. This is (at least) three separate atomic operations:

  • read value
  • increase value
  • write value

This is the classic multi-writer race condition. Another thread might read between "read value" and "write value." In your example the final result could be 4, such that the increase operation is completely lost (A reads 5, B reads 5, A +2, A writes 7, B -1, B writes 4).

The problem that atomic is meant to solve is that "read value" and "write value" aren't even atomic operations in many platform-specific situations. The above may actually be 5 operations such as:

  • read lower word
  • read upper word
  • increase value
  • write lower word
  • write upper word

Without atomic, another thread might read between "write lower word" and "write upper word" and get a garbage value that was never written (half of one value and half of another). By using atomic, you ensure that readers will always receive a "legitimate" value (one that was written at some point). But that's not much of a promise.

But as is noted in the questions you provide, if you need to make reads and writes atomic, you almost certainly need more than that, because you also want to make "increase the value" atomic. So in practice atomic is rarely useful. If you don't need it, it's slow. If you do need it, it's probably insufficient.

Upvotes: 5

Enrique Bermúdez
Enrique Bermúdez

Reputation: 1760

Ok, threads might not execute sequentially, the first thread created might run after the second one. If the threads executes in the order you describe, the mentioned behavior is fine. But I think that your have a misconception about thread safe.

I encourage you to read more about Concurrency Programming Guide and Thread safe.

Upvotes: 0

Related Questions