Alex Rivera | Logout

Objective C: Atomic decrement of integer without @synchronized?

Asked 2012-06-11T21:18:53.960
9

If I use

@property (atomic,assign) int value;

and later access it like so

self.value--;

is that decrement atomic? Because if I had to do this:

self.value = self.value - 1;

then I am sure there would be a chance of a race condition between the read and the write.

My instinct of course is to just do this

@synchronized(self) { value--; }

but I am being told that is not kosher.

Thanks.

Edit
Report

1 Answer

14

Try OSAtomicIncrement and OSAtomicDecrement as described in this article from Apple.

self.value = self.value - 1;          

will not be atomic regardless of how the property is defined.

answered 2012-06-11T22:20:01.773

Your Answer