简介

当一个线程访问数据时,而其他数据不能进行访问,保证线程安全或者可以理解为执行多线程,对于共享资源访问时保证互斥的要求

文章

不再安全的 OSSpinLock

iOS开发中的11种锁以及性能对比

iOS 十种线程锁

iOS多线程篇-NSThread-synchronized(互斥锁)

分类

1.自旋锁:是用于多线程同步的一种锁,线程反复检查锁变量是否可用(一直进行do while忙等)

2.信号量:可以有更多的取值空间,用来实现更加复杂的同步,而不单单是线程间互斥

3.互斥锁:是一种用于多线程编程中,防止两条线程同时对同一公共资源(比如全局变量)进行读写的机制

4.条件锁:当进程的某些资源要求不满足时就进入休眠,也就是锁住了。当资源被分配到了,条件锁打开,进程继续运行

5.递归锁:在同一线程上该锁是可重入的,对于不同线程则相当于普通的互斥锁

6.读写锁:对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作

OSSpinLock

- (void)osspinlock:(int)count{
NSTimeInterval begin, end;
OSSpinLock lock = OS_SPINLOCK_INIT;
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
OSSpinLockLock(&lock);
OSSpinLockUnlock(&lock);
}
end = CACurrentMediaTime();
printf("OSSpinLock: %8.2f msn", (end - begin) * 1000);
}

os_unfair_lock(iOS10之后替代OSSPinLock的锁,解决了优先级反转的问题)

- (void)os_unfair_lock:(int)count{
if (@available(iOS 10.0, *)) {
NSTimeInterval begin, end;
os_unfair_lock_t unfairLock;
unfairLock = &(OS_UNFAIR_LOCK_INIT);
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
os_unfair_lock_lock(unfairLock);
os_unfair_lock_unlock(unfairLock);
}
end = CACurrentMediaTime();
printf("os_unfair_lock: %8.2f msn", (end - begin) * 1000);
}
}
信号量

dispatch_semaphore

- (void)dispatch_semaphore:(int)count{
NSTimeInterval begin, end;
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 msn", (end - begin) * 1000);
}
互斥锁

NSLock

  • NSLock是最常使用的互斥锁,遵循NSLocking协议,通过lock和unlock来完成锁定和解锁*
- (void)nslock:(int)count{
NSTimeInterval begin, end;
NSLock *lock = [NSLock new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
printf("NSLock: %8.2f msn", (end - begin) * 1000); }

pthread_mutex

- (void)pthread_mutex:(int)count{
NSTimeInterval begin, end;
pthread_mutex_t lock;
pthread_mutex_init(&lock, 大专栏  iOS数据锁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 msn", (end - begin) * 1000); }

synchronized

- (void)synchronized:(int)count{
NSTimeInterval begin, end;
NSObject *lock = [NSObject new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
@synchronized(lock) {}
}
end = CACurrentMediaTime();
printf("@synchronized: %8.2f msn", (end - begin) * 1000); }
条件锁

NSCondition

- (void)_NSCondition:(int)count{
NSTimeInterval begin, end;
NSCondition *lock = [NSCondition new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
printf("NSCondition: %8.2f msn", (end - begin) * 1000);
}

NSConditionLock

- (void)_NSConditionLock:(int)count{
NSTimeInterval begin, end;
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 msn", (end - begin) * 1000); }
递归锁

NSRecursiveLock

- (void)_NSRecursiveLock:(int)count{
NSTimeInterval begin, end;
NSRecursiveLock *lock = [NSRecursiveLock new];
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
[lock lock];
[lock unlock];
}
end = CACurrentMediaTime();
printf("NSRecursiveLock: %8.2f msn", (end - begin) * 1000);
}

pthread_mutex_recursive

- (void)pthread_mutex_recursive:(int)count{
NSTimeInterval begin, end;
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 msn", (end - begin) * 1000); }
读写锁

pthread_rwlock

- (void)pthread_rwlock:(int)count{
NSTimeInterval begin, end;
pthread_rwlock_t rwlock;
pthread_rwlock_init(&rwlock,NULL);
begin = CACurrentMediaTime();
for (int i = 0; i < count; i++) {
pthread_rwlock_rdlock(&rwlock);
pthread_rwlock_unlock(&rwlock);
}
end = CACurrentMediaTime();
printf("pthread_rwlock: %8.2f msn", (end - begin) * 1000);
}

总结

通过各种常见锁介绍和性能评测,可以看出要是没有优先级反转的问题的话,osspinlock为最优,其次就是dispatch_semaphore,dispatch_semaphore和os_unfair_lock差距很小,然后就是pthread_mutex。

代码下载

TJLock


iOS数据锁的更多相关文章

  1. iOS数据持久化-OC

    沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...

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

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

  3. iOS数据持久化方式及class_copyIvarList与class_copyPropertyList的区别

    iOS数据持久化方式:plist文件(属性列表)preference(偏好设置)NSKeyedArchiver(归档)SQLite3CoreData沙盒:iOS程序默认情况下只能访问自己的程序目录,这 ...

  4. iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)

    本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...

  5. iOS开发笔记-swift实现iOS数据持久化之归档NSKeyedArchiver

    IOS数据持久化的方式分为三种: 属性列表 (plist.NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data.第三方类库等 归档(又名 ...

  6. iOS数据存储之对象归档

    iOS数据存储之对象归档 对象归档 对象归档是iOS中数据持久化的一种方式. 归档是指另一种形式的序列化,但它是任何对象都可以实现的更常规的类型.使用对模型对象进行归档的技术可以轻松将复杂的对象写入文 ...

  7. iOS数据存储之属性列表理解

    iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起 ...

  8. iOS数据本地化

    本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...

  9. IOS数据持久化之归档NSKeyedArchiver

    IOS数据持久化的方式分为三种: 属性列表 (自定义的Property List .NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data ...

随机推荐

  1. mysql数据库大规模数据读写并行时导致的锁表问题

    问题介绍 最近在给学校做的一个项目中,有一个功能涉及到考核分数问题. 我当时一想,这个问题并不是很难,于是就直接采用了这样的方法:拿着一个表中的数据作为索引,去挨个遍历相关表中的数据,最后经过算分的过 ...

  2. 洛谷 P1379 八数码难题(map && 双向bfs)

    题目传送门 解题思路: 一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC. AC代 ...

  3. git 知识罗列

    git pull is basically a shortcut for two operations: git fetch which downloads the history from the ...

  4. catalina.out日志膨胀问题解决实例,日志门面commons-logging的实践

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 笔者在公司的时候,遇到一个问题,2个模块A.B Tomcat中的catalina.out及catalina ...

  5. java线程——线程基础

    一,线程之间的关系 线程之间存在两种关系: (1)间接相互制约:相互争夺线程资源: (2)直接相互制约:线程之间的相互合作: 间接相互制约也可以成为互斥,直接相互制约也可以称为同步:同步也包括互斥,互 ...

  6. Centos7.6部署rsyslog+loganalyzer+mysql日志管理服务器

    参考来自: the_script :https://yq.aliyun.com/articles/675198 名山.深处:https://www.cnblogs.com/skychenjiajun/ ...

  7. UML-如何画通信图?

    1.链 2.消息 3.自身传递消息 4.消息顺序编号 5.有条件消息 6.互斥的有条件消息 7.循环或迭代 8.调用静态方法 9.多态 10.同步和异步调用

  8. day59-mysql-存储过程、函数、事务、锁、备份

    存储过程.函数不是重要的内容. 三. 存储过程:类似于函数(方法),简单的说存储过程是为了完成某个数据库中的特定功能而编写的语句集合, 该语句集包括SQL语句(对数据的增删改查).条件语句和循环语句等 ...

  9. IDEA查看代码最近修改人及日期

    如图,行号上右键,点击Annotate.即可查看

  10. python-day5爬虫基础之正则表达式2

    dot: '.'匹配任意的字符 '*'匹配任意多个(0到多个) 如图所示, 程序运行结果是abc,之所以没有匹配\n,是因为\n是换行符,它就代表这个字符串是两行的,而正则表达式是一行一行去匹配的.在 ...