I am designing a singleton class in Objective-C which will be accessed by multiple threads. There are 3-4 NSMutableArray
s in my class, outside classes have access with read, add and remove operations which is of course wrapped in this class.
As NSMutableArray
is not thread safe, I am using @synchronized()
to make my operations thread safe, however it causes too much usage of @synchronized()
blocks.
Because for 3-4 array for each I have at least 1 add function, 1 remove function and 5 times I need to read the values. So, for 1 array I am using at least 7 @synchronized()
blocks.
For 4 arrays, I need to add 28 @synchronized
blocks in my singleton class.
Is there any better way to approach my problem?
or, If I do use too all these @synchronized
directives, will it causes problem ?
I know that if I want to make my objects thread safe it will slow down my code, but besides that is there any drawback?