NSMultiReadUniWriteLock is an Objective-C wrapper of the POSIX read-write lock (pthread_rwlock_t), for example,
@interface threadSafeMutableDictionary {
NSMutableDictionary* _dict;
NSMultiReadUniWriteLock* _rwlock;
}
-(void)setObject:(id)obj forKey:(id)key;
-(id)objectForKey:(id)key;
...
@end
@implementation threadSafeMutableDictionary
-(id)init {
if ((self = [super init])) {
_rwlock = [[NSMultiReadUniWriteLock alloc] init];
...
}
return self;
}
...
-(void)setObject:(id)obj forKey:(id)key {
[_rwlock lockForWriting];
[_dict setObject:obj forKey:key];
[_rwlock unlock];
}
-(id)objectForKey:(id)key {
[_rwlock lockForReading];
id obj = [_dict objectForKey:key];
[_rwlock unlock];
return obj;
}
@end
Note that -lockForReadingBeforeDate: and -lockForWritingBeforeDate: are currently implemented as a no-op.
References
- What is a read-write lock: http://en.wikipedia.org/wiki/Read/write_lock_pattern
- Header: http://ericasadun.com/iPhoneDocs300/_n_s_multi_read_uni_write_lock_8h-source.html
|