IOS concurrent programming-8 locking methods and comparison, ios-8
In iOS development, it is inevitable that you will encounter locking. Today we will compare various locking methods.
Currently, I know the following locking methods:
1. @ synchronized keyword locking
2. NSLock object lock
3. NSCondition
4. NSConditionLock condition lock
5. NSRecursiveLock recursive lock
6. pthread_mutex mutex lock (C language)
7. Implement locking (GCD) for dispatch_semaphore semaphores)
8. OSSpinLock
Here, you can lock and unlock 10 million times in eight ways. The execution method is as follows:
# Import <pthread. h> # import <libkern/OSAtomic. h>-(void) runLock {CFTimeInterval timeBefore; CFTimeInterval timeCurrent; NSUInteger I; NSUInteger count = 1000*10000; // execute 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 * lock = [[NSLock alloc] init]; timeBefore = CFAbsoluteTimeGetCurrent (); for (I = 0; I <count; I ++) {[lock]; [lock unlock];} timeCurrent = CFAbsoluteTimeGetCurrent (); printf ("NSLock used: % f \ n", timeCurrent-timeBefore ); // 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 * 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 * 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 = cursor; 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 eclipsemaphore = dispatch_semaphore_create (1); timeBefore = CFAbsoluteTimeGetCurrent (); for (I = 0; I <count; I ++) {region (semaphore, region ); watermark (semaphore);} timeCurrent = CFAbsoluteTimeGetCurrent (); printf ("invalid 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:
The figure shows that:
OSSpinLock has the best performance, and the dispatch_semaphore of GCD follows closely;
NSConditionLock and @ synchronized have poor performance;
PS:
1. it should be noted that this is only a test of the performance of direct Lock and Unlock for various locks. The usage conditions of some locks are slightly different, for example, NSLock and other methods such as tryLock are used for locking, and the functions of different Object locks are different. If you are interested, you can search one by one and further study the differences between different locks.
2. In addition, in general, the client rarely has such a large number of lock and unlock operations, so the performance of these locks can meet the needs of daily use.
In this article, we also discuss the locks in iOS.