Search code examples
multithreadingcocoasynchronizationnslock

Lock and unlock a semaphore from different threads in Cocoa


I need to use some kind of semaphore to protect the access to a mutex zone, but I need this zone to span multiple threads. This is what I found in the documentation:

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.

I remember I read around that this is a way to prevent programming errors. Is this statement correct? I read the threading guide from the documentation but I can't find any other class to use that allows lock and unlock from different threads. Is there any other solution in Cocoa to do this? Thanks!


Solution

  • NSCondition.