NSLock C Object: Lock and Unlock NSLock on Different Threads

I need to install the mutex first to make an asynchronous request, and then unlock the mutex in the callback of this request, which is in another thread.

Apple documentation says:

A warning. The NSLock class uses POSIX threads to implement its locking behavior. When sending an unlock message to an NSLock object, you must ensure that the message is sent from the same stream that sent the initial lock message. Unlocking the lock with another thread may result in undefined.

How can I avoid this undefined behavior and make it work as expected?

+4
source share
3 answers

Even better; use NSOperationQueue or the GCD queue as your synchronization primitive.

Locks are expensive, and semaphores are more or less blocked by a counter.

Queue-based coding is much more efficient, especially when using the built-in queue mechanisms.

+6
source

Do not use a mutex for this. Use a semaphore initialized to 1 or some other blocking mechanism that allows interconnection / blocking.

Rgds, Martin

+3
source

Use NSCondition for this to signal to other streams that they can safely transfer now.

+3
source

All Articles