It is rare anymore that NSLock
is the right tool for the job. There much better tools now, particularly with GCD; more later.
As you probably already know from the docs, but I'll repeat for those reading along:
Warning: The NSLock class uses POSIX threads to implement its locking behavior. When sending an unlock message to an NSLock object, you must be sure that message is sent from the same thread that sent the initial lock message. Unlocking a lock from a different thread can result in undefined behavior.
That's very hard to implement without deadlocking if you're trying to lock and unlock on different threads. The fundamental problem is that if lock
blocks the thread, then there is no way for the subsequent unlock
to ever run on that thread, and you can't unlock
on a different thread. NSLock
is not for this problem.
Rather than NSLock
, you can implement the same patterns with dispatch_semaphore_create()
. These can be safely updated on any thread you like. You can lock using dispatch_semaphore_wait()
and you can unlock using dispatch_semaphore_signal()
. That said, this still usually isn't the right answer.
Most resource contention is best managed with an operation queue or dispatch queue. These provide excellent ways to handle work in parallel, manage resources, wait on events, implement producer/consumer patterns, and otherwise do almost everything that you would have done with an NSLock
or NSThread
in the past. I highly recommend the Concurrency Programming Guide as an introduction to how to design with queues rather than locks.