There are 3 main ways of multi-threaded synchronous Lock: Nslock (normal lock), nscondition (State Lock), synchronized synchronous code block
There are also fewer nsrecursivelock (recursive locks), nsconditionlock (conditional locks), nsdistributedlock (distributed locks).
difference:both Nslock and nscondition implement the <NSLocking> protocol, while the synchronized (lock) {...} uses a synchronous locking method to lock, as long as the lock is unique, the general setting is the current object or class name. Nslock and nscondition are explicit locks, synchronized synchronous code blocks are implicitly locked. The agreement is as follows:@protocol nslocking
-(void) lock; Locking
-(void) unlock; Unlock@end Detailed Introduction nslock:<nslocking>-------------General Lock<1> Properties: @property (copy) NSString *name//Lock name<2> Other methods:-(BOOL) Trylock; Try locking (there may have been other lines Cheng)
-(BOOL) Lockbeforedate: (NSDate *) limit; Timing Lock
Introduction to nscondition:<nslocking>---------State lock<1> Properties: @property (copy) NSString *name//Lock name<2> Other methods:-(void) wait; Sleep waiting to unlock
-(BOOL) Waituntildate: (NSDate *) limit; Timed Waiting for unlocking
-(void) signal; Lock Signal-(void) broadcast; Unlock Notifications Introduction nsrecursivelock:<nslocking>------Recursive lock<1> Properties: @property (copy) NSString *name//Lock name<2> Other methods:-(BOOL) Trylock; Try locking (may have been locked by another thread loads, cannot be locked at this time)
-(BOOL) Lockbeforedate: (NSDate *) limit; Timing Lock
Introduction nsconditionlock:<nslocking>------Condition Lock<1> Properties: @property (copy) NSString *name//Lock name@property (readonly) Nsinteger condition;//Status<2> Other methods:-(BOOL) Trylock; Try locking (there may have been other lines Cheng)
- (BOOL) lockbeforedate: (NSDate *) limit; //Timing plus lock
-(Instancetype) Initwithcondition: (Nsinteger) condition//initialization
-(void) Lockwhencondition: (Nsinteger) condition; Locking
-(BOOL) Trylockwhencondition: (Nsinteger) condition; Try to unlock
-(void) Unlockwithcondition: (Nsinteger) condition; Unlock
-(BOOL) Lockwhencondition: (nsinteger) condition Beforedate: (nsdate *) limit; Timing Lock
Use a simple demo:1, the use of Nslock: First step: Create a Lock ObjectNslock *mylock = [[Nslock alloc]init];While (true){Step Two: Lock the shared preemption resource[Mylock lock]; {.......... Code that operates on a shared preemption resource .... } Step Three: Run the data, release the lock immediately, and call the operation to the other thread[Mylock unlock]; ....................} 2, Nscondition and Nsrecursivelock use and Nslock basically the same, but Nsrecursivelock will be called repeatedly, here nscondition can be introduced. First step: Create a Lock Objectnscondition *mylock = [[Nscondition alloc]init];While (true){Step Two: Lock the shared preemption resource[Mylock lock]; {.......... Code that operates on a shared preemption resource ..... Step Three: Run the data, release the lock immediately, and call the operation to the other thread[Mylock unlock]; ....................}3, the use of synchronized synchronous code block:put the shared preemption resource and execution code in itWhile (true){@synchronized (self) {.......... Code that operates on a shared preemption resource ..... ....................}Description: 4, the use of conditional lock Nsconditionlock :First Step: Set conditions (Initialize)Nsconditionlock *conlock = [[Nsconditionlock alloc] initwithcondition:no_data];Step two: Unlock the thread one lock (producer)While (true) {[Conlock lockwhencondition:no_data];//Locking {.......... Code that operates on a shared preemption resource .....[Conlock unlockwhencondition:has_data];//unlock, at this time the conditions condition set for the internal data, you can customize the settings }Step three: Unlock the Wire Cheng Lock (consumer)While (true) {[Conlock lockwhencondition:has_data];//Locking {.......... Code that operates on a shared preemption resource .....[Conlock unlockwhencondition:no_data];//Unlock } 5, the use of distributed lock NsdistributedlockNsdistributedlock, distributed lock, file way implementation, can cross-process
Use the Trylock method to obtain the lock.
Use the Unlock method to release the lock.
If a process that acquires a lock is hung before releasing the lock, the lock will not be released until the lock can be forcibly acquired through Breaklock.
IOS: A simple introduction to multi-threaded synchronization plus lock