0

Apple documentation says that Atomic operations does not block the competing threads. But, how can it return/set whole value without blocking the competing threads. For example, when Thread 3(T3) is in the middle of getter if both thread1 and thread2 calls setter methods (since does not block competing threads both T1 and T2 are allowed to enter setter method) won't it return invalid value?

I have gone through many web-sites that explain atomicity with example. The examples use @synchronized(self) i setter and getter. Won't @synchronized() allow only single thread to execute a setter at a time blocking other threads?

halfer
  • 19,824
  • 17
  • 99
  • 186
spd
  • 2,114
  • 1
  • 29
  • 54
  • Which documentation are you referencing? – Wain Sep 03 '13 at 07:06
  • Can you please let us know what exactly your confusion is? Atomicity or blocking other threads? Also are you thinking about Atomicity referring to `nonatomic` and `atomic` keywords? – Parth Bhatt Sep 03 '13 at 07:08
  • Following is the link: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW2 – spd Sep 03 '13 at 07:08
  • @ParthBhatt I conceptually understood Atomicity. But, I did not understand how it is achieved. Also, how it is achieved without blocking competing threads. – spd Sep 03 '13 at 07:09
  • 1
    @spd: Here we have atomic and nonatomic scope for variables. You can understand it better here: [Atomic vs nonatomic properties](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties). Here I would recommend you to go through each answer. As all of them are worth reading. – Parth Bhatt Sep 03 '13 at 07:12
  • @spd: One more useful link: [What does the property “Nonatomic” mean?](http://stackoverflow.com/questions/821692/what-does-the-property-nonatomic-mean). You can read Jesse Rusak's answer in this post. Hope this one clarifies your doubts more. – Parth Bhatt Sep 03 '13 at 07:16
  • @ParthBhatt I have gone through this documentation. It well explains the difference between Atomicity and Thread Safety. My query is 'Does @ synchronized will not block competing threads. i.e when ThreadB is in the middle of setter can ThreadC enter setter method? Also when Thread A is in the middle of getter can ThreadB enter setter's synchronized block? – spd Sep 03 '13 at 07:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36709/discussion-between-parth-bhatt-and-spd) – Parth Bhatt Sep 03 '13 at 08:23

0 Answers0