objective c - Doubts on concurrency with objects that can be used multiple times like formatters -


maybe stupid question ask need confirmations on it.

usually, when deal objects can used multiple times within application use approach following.

create extension, example nsdecimalnumber+extension, or class utility number formatter created following.

+ (nsnumberformatter*)internal_sharednumberformatter {     static nsnumberformatter* _internal_numberformatter = nil;     static dispatch_once_t oncetoken;     dispatch_once(&oncetoken, ^{         _internal_numberformatter = [[nsnumberformatter alloc] init];         // other configurations here...     });      return _internal_numberformatter; }  + (nsstring*)stringrepresentationofdecimalnumber:(nsdecimalnumber*)numbertoformat {     nsstring *stringrepresentation = [[self class] internal_sharednumberformatter] stringfromnumber:numbertoformat];     return stringrepresentation; } 

this approach quite since, example, formatters expensive create. applied other situations well.

now, questions following.

does approach valid in situations different path of execution (different threads) involved?

so, if call first stringrepresentationofdecimalnumber on main thread , in different thread, happen?

i think valid perform different calls stringrepresentationofdecimalnumber in different threads since shared formatter, in case, reading only, have reply experts.

thanks in advance.

nsnumberformatter mutable, not thread safe , cited in thread safety summary (see "thread-unsafe classes" section) in non thread safe classes list.

but if treat object immutable object, don't have worry race conditions. example, cannot change format if there multiple threads accessing formatter. if _internal_numberformatter isn't altered in way, , have these 2 methods in category, should consider thread safe.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -