O'Reilly logo

Advanced Mac OS X Programming: The Big Nerd Ranch Guide by Mark Dalrymple

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

For the More Curious: Read/Write Locks

Both Cocoa and pthreads provide read/write locks. These allow a data structure to be read by a number of readers simultaneously but only allow one thread write access at a time.

pthread_rwlock_t works just like pthread_mutex_t but with a couple of extra calls. Here are the details:

pthread_rwlock_init (pthread_rwlock_t *lock,
                     const pthread_rwlockattr_t *attr);

OS X does not define PTHREAD_RWLOCK_INITIALIZER, so you cannot create them statically.

int pthread_rwlock_destroy (pthread_rwlock_t *lock);

Rather than use lock(), you specify which kind of locking you want:

  • int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
  • int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock);
  • int pthread_rwlock_wrlock (pthread_rwlock_t ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required