I can't seem to find any read-write locks for Objective C. This is for iphone dev. Any ideas? The appendix in this paper has some code, but it is incomplete.
Asked
Active
Viewed 1,776 times
2 Answers
0
Foundation provides NSLock to help you lock and unlock threads but I think it could help you.

albianto
- 4,092
- 2
- 36
- 54
-
And `NSRecursiveLock` for recursive locking. Most use cases are easier to implement with recursive locks. :) – Jonathan Grynspan Apr 28 '11 at 19:11
-
1Thanks, but I need a *readers-writer* lock, not just any old lock – Jacko May 01 '11 at 00:52
-
2I searched along internet, I think you can only use pthread_rwlock_t and its related commands – albianto May 01 '11 at 04:06
0
You can use dispatch_barrier_async
function and a concurrent queue to implement a read-write lock.
dispatch_queue_t queue = dispatch_queue_create("your queue name", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
// execute read task 1
});
dispatch_async(queue, ^{
// execute read task 2
});
dispatch_barrier_async(queue, ^{
// execute write task 1
});

Michael
- 415
- 6
- 16