- Nsrecursivelock Recursive lock
The so-called recursive lock, that is, on the same thread, the lock is reentrant, it is equivalent to a common mutex for different threads. A lock defined by the Nsrecursivelock class can be locked multiple times on the same thread without causing a deadlock. A recursive lock keeps track of how many times it is lock. Each successful lock must balance the call unlock operation. Only when all locked and unlocked operations are balanced does the lock really get released to other threads.
Nsrecursivelock *Lock= [[Nsrecursivelock alloc] init];//Open a threadDispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{Static void(^recursivemethod) (int); Recursivemethod = ^ (int value) { [Lock Lock];//Start a recursive lock if(value>0) {NSLog (@ "value =%d",value); Sleep2); Recursivemethod (value-1); } [LockUnlock];//Turn off recursive locks}; Recursivemethod (5); });//Open another thread Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{Sleep2);//Determine the status of the current lockBOOL flag = [LockLockbeforedate:[nsdate Datewithtimeintervalsincenow:1]];if(flag) {NSLog (@ "Lock before date"); [LockUnlock]; }Else{NSLog (@ "fail to lock before date"); } });
Code Analysis: The first asynchronous dispatch creates a new thread and joins a recursive lock, at which point the state of the current recursive lock is judged in the new thread created by the second asynchronous dispatch.
2. Nsconditionlock Condition Lock
The common mutex and recursive lock only consider the problem of lock and unlock, such a lock can not meet the various requirements of multi-threaded access to shared resources. Therefore, the presence of conditional locks (nsconditionlock) can solve scenarios where certain conditions need to be met in multithreaded technology to unlock some locks.
//In the main threadNsconditionlock *conditionlock = [[Nsconditionlock alloc] init];//Async thread 1 Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{ for(intI=0; i<=2; i++) {//Create a lock that does not require a condition[Conditionlock Lock];NSLog(@"thread1:%d", i); Sleep2); [Conditionlock unlockwithcondition:i]; } });//Async thread 2Dispatch_async(Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{//Set a conditional lock that requires the corresponding unlockwithcondition:2 to unlock[Conditionlock lockwhencondition:2];NSLog(@"Thread2"); [Conditionlock unlock]; });
Code Analysis: Asynchronous thread 1 creates a conditional lock without unlocking conditions, and asynchronous thread 2 concurrently creates a conditional lock with a lock condition of 2, and when the loop variable in thread 1 i=2, the conditional lock in thread 2 is untied.
3. Nsdistributedlock Distributed Lock
When you need to build mutually exclusive scenarios between multiple processes or multiple programs, using the locks between the threads above will not meet the requirements. At this point, you need to use a distributed lock. Distributed locks are implemented through the file system, do not inherit from Nslock, and thus do not implement the Lock method, but it provides methods such as Trylock,unlock,breaklock, if lock is required, you must implement a Trylock polling yourself.
Application A:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//进程间互斥所用的锁NSDistributedLock *lock = [[NSDistributedLock alloc] initWithPath:@"/Users/mac/Desktop/earning__"]; [lock breakLock]; [lock tryLock]; sleep(10); [lock unlock]; NSLog(@"appA: OK"); });
Application B:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDistributedLock *lock = [[NSDistributedLock alloc] initWithPath:@"/Users/mac/Desktop/earning__"]; while (![lock tryLock]) { NSLog(@"appB: waiting"); sleep(1); } [lock unlock]; NSLog(@"appB: OK"); });
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The use of various locks in IOS/MAC development