objective c - nonatomic in multi threaded iOS environment -


most iphone code examples use nonatmoc attribute in properties. involve [nsthread detachnewthreadselector:....]. however, issue if not accessing properties on separate thread?

if case, how can sure nonatomic properties won't accessed on different in future, @ point may forget properties set nonatomic. can create difficult bugs.

besides setting properties atomic, can impractical in large app , may introduce new bugs, best approach in case?

please note these these questions ios , not mac in general.

first,know atomicity not insure thread safety class, generates accessors set , properties in thread safe way. subtle distinction. create thread safe code, need more use atomic accessors.

second, key point know accessors can called background or foreground threads safely regardless of atomicity. key here must never called 2 threads simultaneously. nor can call setter 1 thread while simultaneously calling getter another, etc. how prevent simultaneous access depends on tools use.

that said, answer question, can't know sure accessors won't accessed on thread in future. why thread safety hard, , lot of code isn't thread safe. in general, if youre making framework or library, yeah, can try make code thread safe purposes of "defensive programming", or can leave non-thread safe. atomicity of properties small part of that. whichever choose, though, sure document users of library don't have wonder.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -