iOS 各类锁的性能对比

测试平台:iOS 13.4.1
测试机型:iPhone 7
测试结果:

OSSpinLock: 0.14 ms
dispatch_semaphore: 0.16 ms
pthread_mutex: 0.35 ms
NSCondition: 0.27 ms
NSLock: 0.42 ms
pthread_mutex(recursive): 0.32 ms
NSRecursiveLock: 0.66 ms
NSConditionLock: 1.28 ms
@synchronized: 0.90 ms
---- fin (1000) ----

OSSpinLock: 0.76 ms
dispatch_semaphore: 0.95 ms
pthread_mutex: 1.05 ms
NSCondition: 1.60 ms
NSLock: 1.74 ms
pthread_mutex(recursive): 1.61 ms
NSRecursiveLock: 2.86 ms
NSConditionLock: 5.43 ms
@synchronized: 4.21 ms
---- fin (10000) ----

OSSpinLock: 12.82 ms
dispatch_semaphore: 8.62 ms
pthread_mutex: 9.73 ms
NSCondition: 12.93 ms
NSLock: 10.20 ms
pthread_mutex(recursive): 11.15 ms
NSRecursiveLock: 19.93 ms
NSConditionLock: 22.44 ms
@synchronized: 21.11 ms
---- fin (100000) ----

OSSpinLock: 69.89 ms
dispatch_semaphore: 58.54 ms
pthread_mutex: 42.54 ms
NSCondition: 35.49 ms
NSLock: 36.25 ms
pthread_mutex(recursive): 40.94 ms
NSRecursiveLock: 54.75 ms
NSConditionLock: 95.39 ms
@synchronized: 119.96 ms
---- fin (1000000) ----

OSSpinLock: 293.03 ms
dispatch_semaphore: 321.75 ms
pthread_mutex: 344.59 ms
NSCondition: 344.84 ms
NSLock: 356.52 ms
pthread_mutex(recursive): 412.57 ms
NSRecursiveLock: 543.84 ms
NSConditionLock: 938.72 ms
@synchronized: 1304.01 ms
---- fin (10000000) ----

- (void)test:(int)count {
    NSTimeInterval begin, end;
    TimeCount += count;
    
    {
        OSSpinLock lock = OS_SPINLOCK_INIT;
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            OSSpinLockLock(&lock);
            OSSpinLockUnlock(&lock);
        }
        end = CACurrentMediaTime();
        printf("OSSpinLock:               %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        dispatch_semaphore_t lock =  dispatch_semaphore_create(1);
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            dispatch_semaphore_wait(lock, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_signal(lock);
        }
        end = CACurrentMediaTime();
        printf("dispatch_semaphore:       %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        pthread_mutex_t lock;
        pthread_mutex_init(&lock, NULL);
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            pthread_mutex_lock(&lock);
            pthread_mutex_unlock(&lock);
        }
        end = CACurrentMediaTime();
        pthread_mutex_destroy(&lock);
        printf("pthread_mutex:            %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        NSCondition *lock = [NSCondition new];
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            [lock lock];
            [lock unlock];
        }
        end = CACurrentMediaTime();
        printf("NSCondition:              %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        NSLock *lock = [NSLock new];
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            [lock lock];
            [lock unlock];
        }
        end = CACurrentMediaTime();
        printf("NSLock:                   %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        pthread_mutex_t lock;
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init(&lock, &attr);
        pthread_mutexattr_destroy(&attr);
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            pthread_mutex_lock(&lock);
            pthread_mutex_unlock(&lock);
        }
        end = CACurrentMediaTime();
        pthread_mutex_destroy(&lock);
        printf("pthread_mutex(recursive): %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        NSRecursiveLock *lock = [NSRecursiveLock new];
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            [lock lock];
            [lock unlock];
        }
        end = CACurrentMediaTime();
        printf("NSRecursiveLock:          %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:1];
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            [lock lock];
            [lock unlock];
        }
        end = CACurrentMediaTime();
        printf("NSConditionLock:          %8.2f ms\n", (end - begin) * 1000);
    }
    
    
    {
        NSObject *lock = [NSObject new];
        begin = CACurrentMediaTime();
        for (int i = 0; i < count; i++) {
            @synchronized(lock) {}
        }
        end = CACurrentMediaTime();
        printf("@synchronized:            %8.2f ms\n", (end - begin) * 1000);
    }
    
    printf("---- fin (%d) ----\n\n",count);
}

可知,当测试次数上升到1万次以上后OSSpinLock、 dispatch_semaphore 、pthread_mutex 性能表现差不多,在1000次这个级别,dispatch_semaphore和OSSpinLock表现差不多。

目前OSSpinLock已经被废弃了,因为有优先级反转的问题存在,会导致有些情况下,性能很差

具体来说,如果一个低优先级的线程获得锁并访问共享资源,这时一个高优先级的线程也尝试获得这个锁,它会处于 spin lock 的忙等状态从而占用大量 CPU。此时低优先级线程无法与高优先级线程争夺 CPU 时间,从而导致任务迟迟完不成、无法释放 lock。这并不只是理论上的问题,libobjc 已经遇到了很多次这个问题了,于是苹果的工程师停用了 OSSpinLock。

综合考虑,最好的还是使用dispatch_semaphore

你可能感兴趣的:(iOS 各类锁的性能对比)