How to block NSLock in a specific thread

I have a property @property NSLock *myLock

And I want to write two methods:

- (void) lock

and

- (void) unlock

These methods lock and unlock myLockaccordingly, and they must do this regardless of which thread or queue called them. For example, thread A may have a name lock, but queue B may be the one who calls unlock. Both of these methods should work properly, without reporting that I am trying to unlock a lock from another thread / queue that blocked it. In addition, they must do this synchronously.

+5
source share
1 answer

, NSLock . , GCD; .

, , , , :

. NSLock POSIX . NSLock , , . undefined.

, . , lock , unlock , unlock . NSLock .

NSLock dispatch_semaphore_create(). , . dispatch_semaphore_wait(), dispatch_semaphore_signal(). , , , .

. , , , / , NSLock NSThread . Concurrency , .

+23

All Articles