上篇多线程(三)我们看了多线程的安全隐患 以及各种锁的简单使用,接下来我们来看看锁的比较、以及自旋锁、互斥锁比较 和读写安全方案等
代码详见 gitHub_Demo
各种锁类别
锁名字 | 锁类别 | 特点 |
---|---|---|
OSSpinLock | 自旋锁 | 等待锁的线程会处于忙等(busy-wait)状态,一直占用CPU资源 |
os_unfair_lock | 互斥锁 | 等待锁的线程会处于休眠状态,并非忙等 |
pthread_mutex | 互斥锁 | 等待锁的线程会处于休眠状态 |
pthread_mutex – 递归 | 递归锁 | 需要初始化锁的属性(attr) |
pthread_mutex –条件 | 条件锁 | 需要设置等待条件(condition) |
NSLock | 互斥锁 | 是对mutex锁的简单的封装 |
NSRecursiveLock | 递归锁 | 是对mutex递归锁简单的封装 |
NSCondition | 条件锁 | 是对mutex和cond的封装 |
NSConditionLock | 条件锁 | 是对NSCondition更进一步的封装(可以设置具体的条件值) |
SerialQueue | GCD | 可以实现线程同步 |
dispatch_semaphore | GCD | 信号量 |
@synchronized | 互斥锁 | 是对mutex递归锁的封装 |
pthread_rwlock | 读写锁 | 多读单写 |
dispatch_barrier_async | GCD | 异步栅栏调用 |
... | ... | ... |
iOS线程同步方案性能比较
自旋锁、互斥锁比较
atomic
nonatomic和atomic
atom:原子,不可再分割的单
atomic:原子性
给属性加上atomic修饰,可以保证属性的setter和getter都是原子性操作,也就是保证setter和gette内部是线程同步的
// 加锁
int a = 10;
int b = 20;
int c = a + b;
// 解锁
如:
- (void)setName:(NSString *)name{
// 加锁
_name = name;
// 解锁
}
- (NSString *)name{
//加锁
return _name;
// 解锁
}
atomic所说的线程安全只是保证了getter和setter存取方法的线程安全,并不能保证整个对象是线程安全的
#import "ViewController_18.h"
@interface ViewController_18 ()
@property (atomic,assign)NSInteger age;
@end
@implementation ViewController_18
- (void)viewDidLoad {
[super viewDidLoad];
//线程A
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for(int i=0;i<20;i++){
self.age = self.age+1;
NSLog(@"--1---age = %ld---%@",(long)self.age,[NSThread currentThread]);
}
});
//线程B
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for(int i=0;i<20;i++){
self.age = self.age+1;
NSLog(@"--2---age = %ld---%@",(long)self.age,[NSThread currentThread]);
}
});
sleep(5);
NSLog(@"lastAge = %ld---%@",(long)self.age,[NSThread currentThread]);
}
@end
打印:
Multithreading[6412:538075] --2---age = 2---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 1---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538074] --1---age = 3---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 4---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538075] --2---age = 5---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 5---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 6---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 7---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538074] --1---age = 9---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 8---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 10---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 11---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 12---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 13---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 14---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 15---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 16---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 17---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 18---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538074] --1---age = 20---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 19---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 21---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 22---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 23---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 24---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 25---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 26---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 27---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 28---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 29---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 30---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 31---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 32---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 33---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 34---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 35---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 36---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538074] --1---age = 37---<NSThread: 0x600003e7f440>{number = 3, name = (null)}
Multithreading[6412:538075] --2---age = 38---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538075] --2---age = 39---<NSThread: 0x600003e70240>{number = 4, name = (null)}
Multithreading[6412:538035] lastAge = 39---<NSThread: 0x600003efed00>{number = 1, name = main}
发现:有2个5
iOS中的读写安全方案
pthread_rwlock
#import "ViewController_19.h"
#import <pthread.h>
@interface ViewController_19 ()
@property (assign, nonatomic) pthread_rwlock_t lock;
@property (nonatomic,assign)int age;
@end
@implementation ViewController_19
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化锁
pthread_rwlock_init(&_lock, NULL);
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i = 0; i < 100; i++) {
dispatch_async(queue, ^{
[self read];
});
dispatch_async(queue, ^{
[self write];
});
}
sleep(5);
NSLog(@"%@--age=%d",[NSThread currentThread],self.age);
}
- (void)read {
pthread_rwlock_rdlock(&_lock);
NSLog(@"%s--%@--age=%d", __func__,[NSThread currentThread],self.age);
pthread_rwlock_unlock(&_lock);
}
- (void)write{
pthread_rwlock_wrlock(&_lock);
self.age+=1;
NSLog(@"%s--%@--age=%d", __func__,[NSThread currentThread],self.age);
pthread_rwlock_unlock(&_lock);
}
- (void)dealloc{
pthread_rwlock_destroy(&_lock);
}
@end
dispatch_barrier_async
#import "ViewController_20.h"
@interface ViewController_20 ()
@property (nonatomic,strong)dispatch_queue_t queue;
@end
@implementation ViewController_20
- (void)viewDidLoad {
[super viewDidLoad];
self.queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 10; i++) {
[self read];
[self read];
[self read];
[self write];
}
}
- (void)read {
dispatch_async(self.queue, ^{
sleep(1);
NSLog(@"read");
});
}
- (void)write{
dispatch_barrier_async(self.queue, ^{
sleep(1);
NSLog(@"write");
});
}
@end
友情链接: