How does one create a thread safe singleton object in objective-c. for eg. If I have a shared data controller which is a singleton object what will happen if two or more threads are accessing it at the same time..? Or these objects have thread safety by default..?
update:
Is it under this scenario that the objects inside the datacontroller's property decide if it is thread safe or not.. ? Like my datacontroller has NSMutableArray and it is set nonatomic it will not be thread safe. What will happen to its value in that case?
update:2
And what actually does @synchronized(self) do..?
If the data controller is not threadsafe, then undefined behavior could happen -- avoid it at all costs =)
NSObjects are definitely not threadsafe by default. Using atomic properties does not make a class threadsafe (added that because it's a popular misconception).
The typical solution would involve making sure that all of your mutable state is protected with appropriate locking (e.g. a mutex or @synchronized).
When I say mutable state, I am referring to an object which can mutate externally or internally. If you are not sure, lock to be sure the types are read or written from multiple threads. This must happen at reading and writing - always. If you have a lot of reading, a readwrite lock may be a better, more specialized lock.
To answer in more detail, you'd have to post some code.
Update
Is it under this scenario that the objects inside the datacontroller's property decide if it is thread safe or not.. ? Like my datacontroller has NSMutableArray and it is set nonatomic it will not be thread safe. What will happen to its value in that case?
Think of it as being transitive. Your NSMutableArray, the objects it holds, and all external references to them must be used in a threadsafe manner, and you have to track all that. Typically, you start by reducing what mutable state you share. Instead of giving clients a reference to the array, give them copies of the elements held by the array. Meanwhile, protect all reads, writes, and element copying with a lock.
For simplicity, I will demonstrate using @synchronize:
@interface MONCookie : NSObject <NSCopying>
- (NSString *)name;
@end
@interface MONDataController : NSObject
{
@private
NSMutableArray * cookies; // << MONCookie[]
}
- (void)addCookie:(MONCookie *)cookie;
- (MONCookie *)cookieWithName:(NSString *)name;
@end
@implementation MONDataController
- (id)init
{
// no lock required here
self = [super init];
if (nil != self) {
cookies = [NSMutableArray new];
}
return self;
}
- (void)dealloc
{
// no lock required here
[cookies release], cookies = nil;
[super dealloc];
}
- (void)addCookie:(MONCookie *)cookie
{
@synchronized(self) { // now accessing cookies - lock required
[cookies addObject:cookie];
}
}
- (MONCookie *)cookieWithName:(NSString *)name
{
MONCookie * ret = nil;
@synchronized(self) { // now accessing cookies - lock required
for (MONCookie * at in cookies) {
if ([at.name isEqualToString:name]) {
ret = [at copy]; // << give them a copy if cookie is not threadsafe
}
}
}
return [ret autorelease];
}
@end
Update 2
@synchronized sets up an object level lock. You can think of it as a recursive (or reentrant) lock exclusive to your instance. It's also quite slow compared to other locking approaches. The code above uses it, and it is threadsafe and equivalent to holding a recursive lock, and locking and unlocking at the @synchronized boundaries.
@interface MONCookie : NSObject <NSCopying>
{
@private
NSRecursiveLock * lock;
}
@end
@implementation MONCookie
- (id)init
{
self = [super init];
if (nil != self) {
lock = [NSRecursiveLock new];
}
return self;
}
- (void)temperatureDidIncrease
{
// ...
}
- (void)bake
{
// use the same lock for everything
// do not mix @synchronized in some places, and use of the lock
// in others. what you use to protect the data must remain consistent
//
// These are equivalent approaches to protecting your data:
{ // @synchronized:
@synchronized(self) {
[self temperatureDidIncrease];
}
}
{ // using the lock:
[lock lock];
[self temperatureDidIncrease];
[lock unlock];
}
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With