iOS开发中常用的锁有如下几种

来比较一下遇到加锁的情况:

1. @synchronized 关键字加锁

2. NSLock 对象锁 
3. NSCondition  
4. NSConditionLock 条件锁 
5. NSRecursiveLock 递归锁 
6. pthread_mutex 互斥锁(C语言) 
7. dispatch_semaphore 信号量实现加锁(GCD)

8. OSSpinLock (暂不建议使用,原因参见这里

//分别使用8种方式加锁 解锁1千万次

- (void)runLock{

CFTimeInterval timeBefore;

CFTimeInterval timeCurrent;

NSUInteger i;

NSUInteger count = 1000*10000;//执行一千万次

//@synchronized

id obj = [[NSObjectalloc]init];;

timeBefore = CFAbsoluteTimeGetCurrent();

for(i=0; i<count; i++){

@synchronized(obj){

}

}

timeCurrent = CFAbsoluteTimeGetCurrent();

printf("@synchronized used : %f\n", timeCurrent-timeBefore);

//NSLock

NSLock *lock = [[NSLockalloc]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 = [[NSConditionalloc]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 = [[NSConditionLockalloc]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 = [[NSRecursiveLockalloc]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);

}

输出结果如下:

 

由图可以发现:
OSSpinLock的性能最好(不建议使用),GCD的dispatch_semaphore紧随其后; 
NSConditionLock和@synchronized性能较差;

注意: 
1. 需要注意的是这里仅仅是对各种锁直接Lock和Unlock的性能测试,其中部分锁的使用条件上还是有细微的差异的,比如NSLock之类的还有tryLock等方法用于加锁,不同对象锁的功能偏向不一样等等,有兴趣的可以逐个搜索再更深入的研究不同锁之间的区别。 
2. 另外,一般来说客户端很少会有这么大量的加锁解锁操作,所以日常来说这些锁的性能都是可以满足使用需求的。

ios 各种锁的使用性能比较的更多相关文章

  1. Delphi IOS 蓝牙锁屏后台运行

    Delphi IOS 后台运行 同样的程序,编译成android,锁屏后继续运行正常,蓝牙通讯正常,但在IOS下锁屏后程序的蓝牙就中断通讯了? IOS的机制就是这样,锁屏就关闭了. 音乐播放器是怎么做 ...

  2. iOS数据锁

    简介 当一个线程访问数据时,而其他数据不能进行访问,保证线程安全或者可以理解为执行多线程,对于共享资源访问时保证互斥的要求 文章 不再安全的 OSSpinLock iOS开发中的11种锁以及性能对比 ...

  3. iOS 线程锁同步机制

    转载自 http://yulingtianxia.com/blog/2015/11/01/More-than-you-want-to-know-about-synchronized/ 如果你已经使用 ...

  4. Delphi IOS MusicPlayer 锁屏运行学习

    [weak] FMusicPlayer: TMusicPlayer; [weak]修饰, 编译器在处理这个变量的时候不会调用该变量内容的__ObjAddRef和__ObjRelease., proce ...

  5. 不会吧,这也行?iOS后台锁屏监听摇一摇

    目录 背景介绍 探索过程 其他 APP 有没有类似功能 系统提供的摇一摇回调能否满足 其他方法能否实现 利用 CoreMotion 框架,监听加速计原始数据 通过加速计监听摇一摇 控制器相关逻辑和代码 ...

  6. iOS - Mac 锁屏快捷键设置

    Mac 锁屏快捷键设置 control + shift + Eject 锁屏快捷键 如果用户要离开电脑一段时间,可以选择直接把笔记本直接合上.但是这样原先在跑的进程就会挂起或者结束,如果正在下载,那么 ...

  7. iOS开发——锁屏监听

    公司所做的项目,锁屏监听是为了60秒后,解锁瓶后显示[手势解锁]或[指纹验证]: 第一步:AppDelegate.m 头部导入 #import <notify.h> #define Not ...

  8. iOS图案锁,支持动画、图片、绘图

    最近忙着搭建一个聊天用的框架,过几天应该会整理写出来吧,原理不难,但是实现后会省很多事.好久没写博客,周末心血来潮写了个图案锁,这东西没什么技术含量,网上一堆,这次这个图案锁顺便联系了怎么打包使用.a ...

  9. iOS - 互斥锁&&自旋锁 多线程安全隐患(转载)

    一.多线程安全隐患 资源共享  一块资源可能会被多个线程共享,也就是多个线程可能会访问到一块资源 比如多个线程访问同一个对象,同一个变量,同一个文件. 当多线程访问同一块资源的时候,很容易引发数据错乱 ...

随机推荐

  1. nagios客户端安装与配置windows篇

    一.被监控的windows xp客户端的配置 1.安装NSClient++并安装下载地址: http://sourceforge.net/projects/nscplusNSClient++-0.3. ...

  2. kkkk

    monkey -p com.alfl.www  -v -v -v  --throttle 50 --pct-touch 30 --pct-motion 15 --pct-nav 15 --pct-ma ...

  3. 杂项-QRCode:ZXing

    ylbtech-杂项-QRCode:ZXing 1.返回顶部 1. ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.Zxing可以实现使用 ...

  4. git pull 冲突

    1. 问题描述 error: Your local changes to the following files would be overwritten by merge: xxx/xxx/xxx ...

  5. 用python监控您的window服务

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://world77.blog.51cto.com/414605/782935 最近比较 ...

  6. webapi 跨域 (MVC-Web API: 405 method not allowed问题 )

    使用webapi cors 1.安装包:Install-Package Microsoft.AspNet.WebApi.Cors –IncludePrerelease 2.在webapiconfig. ...

  7. Crypto Challenge Set 1解题报告

    1.Convert hex to base64 题意:给出一个hex编码过的字符串,将它进行base64加密 解题关键:直接利用base64库函数实现 import base64 str1=" ...

  8. Linux下搭建tomcat集群全记录

    (转) Linux下搭建tomcat集群全记录 2011-10-12 10:23 6133人阅读 评论(1) 收藏 举报 tomcatlinuxapacheinterceptorsession集群 1 ...

  9. 《Java多线程编程核心技术》读后感(十五)

    线程的状态 线程对象在不同的运行时期有不同的状态,状态信息就存在与State枚举类中. 验证New,Runnable,Terminated new:线程实例化后还从未执行start()方法时的状态 r ...

  10. 锐捷认证的一些问题&解决方法

    scau锐捷校园网各种无法吐槽,认证有时候自己掉线了麻痹都打到boss了给我掉线,收费也坑爹,连铁通都比不上. 1.锐捷认证客户端已停止工作: 貌似是毒霸的问题,把金山毒霸关掉再试 2.获取ip地址信 ...