iOS development will inevitably encounter lock-up situation, today to compare the various locking methods.
At the moment I know that there are several ways to lock:
1. @synchronized keyword Lock
2. Nslock Object Lock
3. Nscondition
4. Nsconditionlock Condition Lock
5. Nsrecursivelock Recursive lock
6. Pthread_mutex Mutual Exclusion Lock (C language)
7. Dispatch_semaphore Semaphore realization Locking (GCD)
8. Osspinlock
There are 8 ways to unlock the lock 10 million times respectively, the following method is performed:
#import<pthread.h>#import<libkern/osatomic.h>-(void)runlock{Cftimeinterval Timebefore; Cftimeinterval timecurrent; Nsuinteger i; Nsuinteger count = 1000*10000;//executes 10 million times//@synchronized id obj = [[NSObject alloc]init];; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){@synchronized(obj){}} timecurrent = Cfabsolutetimegetcurrent(); Printf("@synchronized used:%f\n", timecurrent-timebefore); Nslock Nslock *lock = [[Nslock alloc]init]; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){[Lock lock]; [Lock unlock]; } timecurrent = Cfabsolutetimegetcurrent(); Printf("Nslock used:%f\n", timecurrent-timebefore); Nscondition nscondition *condition = [[Nscondition alloc]init]; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){[Condition lock]; [Condition unlock]; } timecurrent = Cfabsolutetimegetcurrent(); Printf("nscondition used:%f\n", timecurrent-timebefore); Nsconditionlock Nsconditionlock *conditionlock = [[Nsconditionlock alloc]init]; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){[Conditionlock lock]; [Conditionlock unlock]; } timecurrent = Cfabsolutetimegetcurrent(); Printf("Nsconditionlock used:%f\n", timecurrent-timebefore); Nsrecursivelock Nsrecursivelock *recursivelock = [[Nsrecursivelock alloc]init]; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){[Recursivelock lock]; [Recursivelock unlock]; } timecurrent = Cfabsolutetimegetcurrent(); Printf("Nsrecursivelock used:%f\n", timecurrent-timebefore); Pthread_mutex pthread_mutex_t mutex = Pthread_mutex_initializer; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){Pthread_mutex_lock(&mutex); Pthread_mutex_unlock(&mutex); } timecurrent = Cfabsolutetimegetcurrent(); Printf("Pthread_mutex used:%f\n", timecurrent-timebefore); Dispatch_semaphore dispatch_semaphore_t semaphore = dispatch_semaphore_create(1); Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){dispatch_semaphore_wait(semaphore, dispatch_time_forever); Dispatch_semaphore_signal(semaphore); } timecurrent = Cfabsolutetimegetcurrent(); Printf("Dispatch_semaphore used:%f\n", timecurrent-timebefore); Osspinlocklock Osspinlock spinlock = os_spinlock_init; Timebefore = Cfabsolutetimegetcurrent(); For(i=0; i<count; i++){Osspinlocklock(&spinlock); Osspinlockunlock(&spinlock); } timecurrent = Cfabsolutetimegetcurrent(); Printf("Osspinlock used:%f\n", timecurrent-timebefore);}
Execution Result:
Found by diagram:
Osspinlock performance is the best, GCD dispatch_semaphore closely followed;
Poor performance of Nsconditionlock and @synchronized;
Ps:
1. Note that this is only a variety of lock directly lock and unlock performance testing, and some of the use of the condition of the lock is still slightly different, such as Nslock and other methods such as Trylock to lock, different object lock function bias, etc. It is interesting to search for the difference between the different locks in more depth.
2. In addition, in general, clients rarely have such a large number of locking unlock operations, so the daily performance of these locks can be used to meet the needs.
This article for the iOS lock is also a discussion, welcome you to discuss the message.
iOS concurrent programming-8 ways to lock and compare