multithreading - If there is two thread calling different methods in objective c, how can avoid that the number will mess up? -
for example here .m:
-(void)counterplusone { [_counter plusone]; } -(void)counterminusone { [_counter minusone]; } there 2 methods may called together, or 1 being called, called well. example, if usercallcountplusone, _counter's plusone method, may lots of thing first. while calculating in counterplusone, counterminusone may being called. so, if these 2 thing go together, _counter's valuables may messed up. how can avoid that? thanks.
try use @synchronized if can't find better solution, because lock in code bad in definition
-(void)counterplusone { @synchronized(_counter) { [_counter plusone]; } } -(void)counterminusone { @synchronized(_counter) { [_counter minusone]; } }
Comments
Post a Comment