For example here is the .m:
-(void)counterPlusOne
{
[_counter plusOne];
}
-(void)counterMinusOne
{
[_counter minusOne];
}
There two methods may be called together, or one being called, another will be called as well. For example, if the userCallcountPlusOne, the _counter
's plusOne
method, may be a lots of thing first. But while the calculating in counterPlusOne
, the counterMinusOne
may also being called. So, if these two thing go together, the _counter
's valuables may be messed up. How can I avoid that? Thanks.
Using @
synchronized
mentioned by @sage444.You can also use
NSRecursiveLock
:And this method used frequently in
ASIHTTPRequest
source.