Goal C: Atom decrement of an integer without @ synchronized?

If i use

@property (atomic,assign) int value; 

and then access it like that

 self.value--; 

is an atom of decrement. Because if I had to do this:

 self.value = self.value - 1; 

then I’m sure there will be a chance of a race condition between reading and writing.

My instinct, of course, is simply to do this.

 @synchronized(self) { value--; } 

but they tell me that this is not a kosher.

Thanks.

+6
source share
1 answer

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

 self.value = self.value - 1; 

will not be an atom, no matter how the property is defined.

+13
source

Source: https://habr.com/ru/post/917830/


All Articles