Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198
NSMultiReadUniWriteLock - iPhone Development Wiki

NSMultiReadUniWriteLock

From iPhone Development Wiki
Revision as of 14:42, 28 October 2009 by KennyTM~ (talk | contribs) (Created page with 'NSMultiReadUniWriteLock is an Objective-C wrapper of the POSIX read-write lock (<tt>pthread_rwlock_t</tt>), for example, <source lang="objc"> @interface threadSafeMutableDict…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 tryLockForWriting];
  [_dict setObject:obj forKey:key];
  [_rwlock unlock];
}
-(id)objectForKey:(id)key {
  [_rwlock tryLockForReading];
  id obj = [_dict objectForKey:key];
  [_rwlock unlock];
  return obj;
}
@end

Note that -lockForReadingBeforeDate: and -lockForWritingBeforeDate: are currently implemented as a no-op.

References