iOS中的常见多线程方案
GCD的常用函数
- GCD中有2个用来执行任务的函数
用同步的方式执行任务
dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);
queue:队列
block:任务
用异步的方式执行任务
dispatch_async(dispatch_queue_t queue, dispatch_block_t block); - GCD源码:https://github.com/apple/swift-corelibs-libdispatch
最简单的GCD的例子:
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
NSLog(@"执行任务 - %@",[NSThread currentThread]);
});
打印结果:执行任务 - <NSThread: 0x6000039accc0>{number = 4, name = (null)}
在子线程中打印
dispatch_sync(queue, ^{
NSLog(@"执行任务 - %@",[NSThread currentThread]);
});
打印结果:执行任务 - <_NSMainThread: 0x600000370a80>{number = 1, name = main}
在当前的线程中打印
GCD的队列
- GCD的队列可以分为2大类型
并发队列(Concurrent Dispatch Queue):可以让多个任务并发(同时)执行(自动开启多个线程同时执行任务);并发功能只有在异步(dispatch_async)函数下才有效
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
NSLog(@"执行任务1 %d - %@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
NSLog(@"执行任务2 %d - %@",i,[NSThread currentThread]);
}
});
打印结果:
2023-04-12 19:52:08.865359+0800 inter_GCD[1789:28975] 执行任务2 0 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.865389+0800 inter_GCD[1789:28972] 执行任务1 0 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.865975+0800 inter_GCD[1789:28972] 执行任务1 1 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.865976+0800 inter_GCD[1789:28975] 执行任务2 1 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866111+0800 inter_GCD[1789:28975] 执行任务2 2 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866071+0800 inter_GCD[1789:28972] 执行任务1 2 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.866169+0800 inter_GCD[1789:28975] 执行任务2 3 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866218+0800 inter_GCD[1789:28975] 执行任务2 4 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866328+0800 inter_GCD[1789:28972] 执行任务1 3 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.866395+0800 inter_GCD[1789:28972] 执行任务1 4 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
串行队列(Serial Dispatch Queue):让任务一个接着一个地执行(一个任务执行完毕后,再执行下一个任务)
dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
NSLog(@"执行任务1 %d - %@",i,[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
NSLog(@"执行任务2 %d - %@",i,[NSThread currentThread]);
}
});
打印结果:
2023-04-12 20:01:32.158700+0800 inter_GCD[1984:39989] 执行任务1 0 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159041+0800 inter_GCD[1984:39989] 执行任务1 1 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159110+0800 inter_GCD[1984:39989] 执行任务1 2 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159444+0800 inter_GCD[1984:39989] 执行任务1 3 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159508+0800 inter_GCD[1984:39989] 执行任务1 4 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159644+0800 inter_GCD[1984:39989] 执行任务2 0 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159719+0800 inter_GCD[1984:39989] 执行任务2 1 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159772+0800 inter_GCD[1984:39989] 执行任务2 2 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159836+0800 inter_GCD[1984:39989] 执行任务2 3 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159892+0800 inter_GCD[1984:39989] 执行任务2 4 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
容易混淆的术语
- 有4个术语比较容易混淆:同步、异步、并发、串行
- 同步和异步主要影响:能不能开启新的线程
同步:在当前线程中执行任务,不具备开启新线程的能力
异步:在新的线程中执行任务,具备开启新线程的能力 - 并发和串行主要影响:任务的执行方式
并发:多个任务并发(同时)执行
串行:一个任务执行完毕后,再执行下一个任务
dispatch_sync和dispatch_async用来控制是否要开启新的线程
队列的类型,决定了任务的执行方式(并发、串行):并发队列、串行队列、主队列(也是一种串行队列)
各种队列的执行效果
- 使用sync函数往当前串行队列中添加任务,会卡住当前的串行队列(产生死锁),代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_sync(queue, ^{
NSLog(@"执行任务2");
});
NSLog(@"执行任务3");
}
打印结果:
2023-04-12 20:24:21.543663+0800 inter_GCD[2395:64891] 执行任务1
(lldb) 崩掉原因:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
问题1:以上代码是在主线程执行的,会不会产生死锁?
答:会产生死锁。队列的特点:排队,FIFO(First In First Out),先进先出,所以当前主队列中正在执行的任务是viewDidLoad这个方法,那么^{ NSLog(@"执行任务2"); })任务应该排在viewDidLoad方法之后,也就是执行完NSLog(@"执行任务3");代码,而dispatch_sync:立马在当前线程执行任务,执行完毕才能继续往下执行NSLog(@"执行任务3");,所以就造成了死锁。
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"执行任务1");
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"执行任务2");
});
NSLog(@"执行任务3");
}
打印结果:
2023-04-12 20:40:46.578575+0800 inter_GCD[2631:81899] 执行任务1
2023-04-12 20:40:46.578792+0800 inter_GCD[2631:81899] 执行任务3
2023-04-12 20:40:46.664453+0800 inter_GCD[2631:81899] 执行任务2
问题2:以上代码是在主线程执行的,会不会产生死锁?
答:不会产生死锁。
dispatch_sync:立马在当前线程同步执行任务
dispatch_async:不要求立马在当前线程同步执行任务
会死锁
- (void)interview01{
NSLog(@"执行任务1");
dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"执行任务2");
dispatch_sync(queue, ^{
NSLog(@"执行任务3");
});
NSLog(@"执行任务4");
});
NSLog(@"执行任务5");
}
不会死锁
- (void)interview01{
NSLog(@"执行任务1");
dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue2 = dispatch_queue_create("myqueue2", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"执行任务2");
dispatch_sync(queue2, ^{
NSLog(@"执行任务3");
});
NSLog(@"执行任务4");
});
NSLog(@"执行任务5");
}
不会死锁
- (void)interview01{
NSLog(@"执行任务1");
dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"执行任务2");
dispatch_sync(queue, ^{
NSLog(@"执行任务3");
});
NSLog(@"执行任务4");
});
NSLog(@"执行任务5");
}
dispatch_queue_t queue0 = dispatch_get_main_queue();//主队列
dispatch_queue_t queue1 = dispatch_get_global_queue(0, 0);//全局并发队列
dispatch_queue_t queue2 = dispatch_queue_create("queue3", DISPATCH_QUEUE_CONCURRENT);//手动创建并发队列
dispatch_queue_t queue3 = dispatch_queue_create("queue4", DISPATCH_QUEUE_SERIAL);//手动创建串行队列
下面代码的打印结果是什么?
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
NSLog(@"1");
[self performSelector:@selector(test) withObject:nil afterDelay:.0];
NSLog(@"3");
});
- (void)test{
NSLog(@"2");
}
打印结果:
1 3
[self performSelector:@selector(test) withObject:nil afterDelay:.0];
这句代码的本质是:往RunLoop里边添加了一个定时器。把当前代码放在主线程中可以走test里边的代码,因为主线程中默认开启RunLoop,而当前是在子线程中,没有默认开启的RunLoop,所以定时器是没法工作的,这句代码在子线程中没有开启RunLoop时是不走的,如果想实现[self performSelector:@selector(test) withObject:nil afterDelay:.0];
代码,做如下修改:
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
NSLog(@"1");
[self performSelector:@selector(test) withObject:nil afterDelay:.0];
NSLog(@"3");
[[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
});
- (void)test{
NSLog(@"2");
}
打印结果:
1 3 2
[self performSelector:(SEL) withObject:(id)]
[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
如上两句代码的本质是不一样的,[self performSelector:(SEL) withObject:(id)]本质就是objc_msgSend(self,SEL),[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]是和RunLoop有关的
GNUstep
- GNUstep是GNU计划的项目之一,它将Cocoa的OC库重新开源实现了一遍
- GNU源码地址
- 虽然GNUstep不是苹果官方源码,但还是具有一定的参考价值
下面代码打印的结果是什么?
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSThread *thread = [[NSThread alloc] initWithBlock:^{
NSLog(@"1");
}];
[thread start];
[self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];
}
- (void)test{
NSLog(@"2");
}
打印结果:
1
*** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[ViewController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'
崩溃原因: [thread start];代码执行完就去子线程中执行^{
NSLog(@"1");
},执行完毕子线程就退出了,下一句代码又要在thread子线程中调用test,所以就崩溃掉了,所以除非添加RunLoop,代码如下:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSThread *thread = [[NSThread alloc] initWithBlock:^{
NSLog(@"1");
[[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}];
[thread start];
[self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];
}
- (void)test{
NSLog(@"2");
}
打印结果:
1 2
队列组的使用
思考:如何用GCD实现以下功能?
- 异步并发执行任务1、任务2
- 等任务1、任务2都执行完毕后,再回到主线程执行任务3
- (void)viewDidLoad {
[super viewDidLoad];
//创建队列组
dispatch_group_t group = dispatch_group_create();
//创建并发队列
dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
//添加异步任务
dispatch_group_async(group, queue, ^{
for (int i=0; i<5; i++) {
NSLog(@"任务1 - %@",[NSThread currentThread]);
}
});
dispatch_group_async(group, queue, ^{
for (int i=0; i<5; i++) {
NSLog(@"任务2 - %@",[NSThread currentThread]);
}
});
//等前面的任务执行完毕后,会自动执行这个任务
dispatch_group_notify(group, queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
for (int i=0; i<5; i++) {
NSLog(@"任务3 - %@",[NSThread currentThread]);
}
});
});
}
多线程的安全隐患
- 资源共享:1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源;比如多个线程访问同一个对象、同一个变量、同一个文件
- 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题
多线程安全隐患示例01 - 存钱取钱
![](https://upload-images.jianshu.io/upload_images/14592702-91a647595f65770a.png?imageMogr 2/auto-orient/strip%7CimageView2/2/w/1240)
- (void)viewDidLoad {
[super viewDidLoad];
[self moneyTest];
}
/*
存钱、取钱演示
*/
- (void)moneyTest{
self.money = 100;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<10; i++) {
[self saveMoney];
}
});
dispatch_async(queue, ^{
for (int i=0; i<10; i++) {
[self drawMoney];
}
});
}
/*
存钱
*/
- (void)saveMoney{
int oldMoney = self.money;
sleep(.2);
oldMoney += 50;
self.money = oldMoney;
NSLog(@"存50,还剩 %d 元 - %@",oldMoney,[NSThread currentThread]);
}
/*
取钱
*/
- (void)drawMoney{
int oldMoney = self.money;
sleep(.2);
oldMoney -= 20;
self.money = oldMoney;
NSLog(@"取20,还剩 %d 元 - %@",oldMoney,[NSThread currentThread]);
}
打印结果:
2023-04-12 22:56:51.003092+0800 inter_GCD[5845:243779] 存50,还剩 130 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.003083+0800 inter_GCD[5845:243777] 取20,还剩 80 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.003621+0800 inter_GCD[5845:243779] 存50,还剩 180 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.003623+0800 inter_GCD[5845:243777] 取20,还剩 160 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.003686+0800 inter_GCD[5845:243779] 存50,还剩 210 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.003743+0800 inter_GCD[5845:243777] 取20,还剩 190 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.003755+0800 inter_GCD[5845:243779] 存50,还剩 240 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.003821+0800 inter_GCD[5845:243777] 取20,还剩 220 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.003940+0800 inter_GCD[5845:243777] 取20,还剩 200 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.004035+0800 inter_GCD[5845:243777] 取20,还剩 230 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.004096+0800 inter_GCD[5845:243779] 存50,还剩 250 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.004114+0800 inter_GCD[5845:243777] 取20,还剩 210 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.004240+0800 inter_GCD[5845:243779] 存50,还剩 260 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.004292+0800 inter_GCD[5845:243777] 取20,还剩 240 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.004353+0800 inter_GCD[5845:243779] 存50,还剩 290 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.007682+0800 inter_GCD[5845:243777] 取20,还剩 270 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.007756+0800 inter_GCD[5845:243777] 取20,还剩 250 元 - <NSThread: 0x600002391a00>{number = 3, name = (null)}
2023-04-12 22:56:51.007843+0800 inter_GCD[5845:243779] 存50,还剩 300 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.007962+0800 inter_GCD[5845:243779] 存50,还剩 350 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
2023-04-12 22:56:51.008056+0800 inter_GCD[5845:243779] 存50,还剩 400 元 - <NSThread: 0x6000023813c0>{number = 6, name = (null)}
多线程安全隐患示例02 - 卖票
- (void)viewDidLoad {
[super viewDidLoad];
[self saleTickets];
}
- (void)saleTicket{
int oldTicketsCount = self.ticketsCount;
sleep(.2);//突出安全隐患问题
oldTicketsCount --;
self.ticketsCount = oldTicketsCount;
NSLog(@"还剩%d张票",oldTicketsCount);
}
- (void)saleTickets{
self.ticketsCount = 15;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self saleTicket];
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self saleTicket];
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self saleTicket];
}
});
}
打印结果:
2023-04-12 22:48:13.588880+0800 inter_GCD[5603:232713] 还剩14张票
2023-04-12 22:48:13.588745+0800 inter_GCD[5603:232712] 还剩13张票
2023-04-12 22:48:13.588813+0800 inter_GCD[5603:232706] 还剩14张票
2023-04-12 22:48:13.589438+0800 inter_GCD[5603:232706] 还剩12张票
2023-04-12 22:48:13.589474+0800 inter_GCD[5603:232712] 还剩11张票
2023-04-12 22:48:13.589493+0800 inter_GCD[5603:232713] 还剩10张票
2023-04-12 22:48:13.589549+0800 inter_GCD[5603:232713] 还剩9张票
2023-04-12 22:48:13.589626+0800 inter_GCD[5603:232713] 还剩8张票
2023-04-12 22:48:13.589680+0800 inter_GCD[5603:232713] 还剩6张票
2023-04-12 22:48:13.589689+0800 inter_GCD[5603:232706] 还剩7张票
2023-04-12 22:48:13.589689+0800 inter_GCD[5603:232712] 还剩7张票
2023-04-12 22:48:13.589829+0800 inter_GCD[5603:232706] 还剩5张票
2023-04-12 22:48:13.589888+0800 inter_GCD[5603:232712] 还剩4张票
2023-04-12 22:48:13.589975+0800 inter_GCD[5603:232706] 还剩3张票
2023-04-12 22:48:13.589986+0800 inter_GCD[5603:232712] 还剩2张票
多线程安全隐患分析
多线程安全隐患的解决方案
- 解决方案:使用线程同步技术(同步,就是协同步调,按预定的先后次序进行运行)
-
常用的线程同步技术:加锁
iOS中的线程同步方案
- OSSpinLock
- os_unfair_lock
- pthread_mutex
- dispatch_semaphore
- dispatch_queue(DISPATCH_QUEUE_SERIAL)
- NSLock
- NSRecursiveLock
- NSConditionLock
- @synchronized
OSSpinLock
- OSSpinLock叫做“自旋锁”,等待锁的线程会处于忙等(busy-wait)状态,一直占用着CPU资源。
卖票代码加锁演示:
#import "ViewController.h"
#import <libkern/OSAtomic.h>
@interface ViewController ()
@property (nonatomic,assign) int ticketsCount;
@property (nonatomic,assign) OSSpinLock lock;//注意:c语言类型的,不能用strong修饰
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化锁
self.lock = OS_SPINLOCK_INIT;
[self ticketTest];
}
/*
卖1张票
*/
- (void)saleTicket{
//加锁
OSSpinLockLock(&_lock);
int oldTicketsCount = self.ticketsCount;
sleep(.2);//突出安全隐患问题
oldTicketsCount --;
self.ticketsCount = oldTicketsCount;
NSLog(@"还剩%d张票",oldTicketsCount);
//解锁
OSSpinLockUnlock(&_lock);
}
/*
卖票演示
*/
- (void)ticketTest{
self.ticketsCount = 15;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self saleTicket];
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self saleTicket];
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self saleTicket];
}
});
}
@end
存钱、取钱加锁代码演示:
#import "ViewController.h"
#import <libkern/OSAtomic.h>
@interface ViewController ()
@property (nonatomic,assign) int money;
@property (nonatomic,assign) OSSpinLock lock1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化锁
self.lock1 = OS_SPINLOCK_INIT;
[self moneyTest];
}
/*
存钱、取钱演示
*/
- (void)moneyTest{
self.money = 100;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<10; i++) {
[self saveMoney];
}
});
dispatch_async(queue, ^{
for (int i=0; i<10; i++) {
[self drawMoney];
}
});
}
/*
存钱
*/
- (void)saveMoney{
//加锁
OSSpinLockLock(&_lock1);
int oldMoney = self.money;
sleep(.2);
oldMoney += 50;
self.money = oldMoney;
NSLog(@"存50,还剩 %d 元 - %@",oldMoney,[NSThread currentThread]);
//解锁
OSSpinLockUnlock(&_lock1);
}
/*
取钱
*/
- (void)drawMoney{
//加锁
OSSpinLockLock(&_lock1);
int oldMoney = self.money;
sleep(.2);
oldMoney -= 20;
self.money = oldMoney;
NSLog(@"取20,还剩 %d 元 - %@",oldMoney,[NSThread currentThread]);
//解锁
OSSpinLockUnlock(&_lock1);
}
@end
- OSSpinLock目前已经不再安全,可能会出现优先级反转问题:如果等待锁的线程优先级较高,它会一直占用着CPU资源,优先级低的线程就无法释放锁.(iOS10.0之后苹果不推荐使用)
- 需要导入头文件
#import <libkern/OSAtomic.h>
OSSpinLock常见函数用法:
//初始化
OSSpinLock lock = OS_SPINLOCK_INIT;
//尝试加锁(如果需要等待就不加锁,直接返回false;如果不需要等待就加锁,返回true)
bool result = OSSpinLockTry(&lock);
//加锁
OSSpinLockLock(& lock);
解锁
OSSpinLockUnlock(& lock);
os_unfair_lock
- os_unfair_lock用于取代不安全的OSSpinLock,从iOS10开始才支持
- 从底层调用看,等待os_unfair_lock锁的线程会处于休眠状态,并非忙等
- 需要导入头文件
#import <os/lock.h>
//初始化
os_unfair_lock lock = OS_UNFAIR_LOCK_INIT;
//尝试加锁
os_unfair_lock_trylock(&lock);
//加锁
os_unfair_lock_lock(&lock);
//解锁
os_unfair_lock_unlock(&lock);
pthread_mutex(pthread开头的一般都是跨平台的)
- mutex叫做“互斥锁”,等待锁的线程会处于休眠状态
- 需要导入头文件
#import <pthread.h>
//初始化锁的属性
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_NORMAL);
//初始化锁
pthread_mutex_t mutex;
pthread_mutex_init(&mutex,&attr);
//尝试加锁
pthread_mutex_trylock(&mutex);
//加锁
pthread_mutex_lock(&mutex);
//解锁
pthread_mutex_unlock(&mutex);
//销毁相关资源
pthread_mutexattr_destroy(&attr);
pthread_mutex_destroy(&mutex);
- pthread_mutex - 条件
条件锁应用场景,线程1需要等待线程2执行完毕再去执行线程1,就可以用条件锁。
//初始化锁
pthread_mutex_t mutex;
//NULL代表使用默认属性
pthread_mutex_init(&mutex,NULL);
//初始化条件
pthread_cond_t condition;
pthread_cond_init(&condition,NULL);
//等待条件(进入休眠,放开mutex锁;被唤醒后,会再次对mutex加锁)
pthread_cond_wait(&condition,&mutex);
//激活一个等待该条件的线程
pthread_cond_signal(&condition);
//激活所有等待该条件的线程
pthread_cond_broadcast(&condition);
//销毁资源
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&condition);
NSLock、NSRecursiveLock
-
NSLock是对mutex普通锁的封装
初始化锁:
NSLock *lock = [[NSLock alloc] init];
- NSRecursiveLock也是对mutex递归锁的封装,API跟NSLock基本一致
NSCondition
-
NSCondition是对mutex和cond的封装
NSConditionLock
-
NSConditionLock是对NSCondition的进一步封装,可以设置具体的条件值
- 能够设置线程之间的依赖
- 如果不设置condition,那么默认为0.
dispatch_queue(DISPATCH_QUEUE_SERIAL)
- 直接使用GCD的串行队列,也是可以实现线程同步的
dispatch_semaphore
- semaphore叫做“信号量”
- 信号量的初始值,可以用来控制线程并发访问的最大数量
-
信号量的初始值为1,代表同时只允许1条线程访问资源,保证线程同步
@synchronized
- @synchronized是对mutex递归锁的封装
- 源码查看:objc4中的objc-sync.mm文件
-
@synchronized(obj)内部会生成obj对应的递归锁,然后进行加锁、解锁操作
把几种加锁代码写到一起进行比较:
先写一个卖票、存钱、取钱的基类,然后再写几种分别加锁的类
BaseDemo.h文件
#import <Foundation/Foundation.h>
@interface BaseDemo : NSObject
/*
卖票演示
*/
- (void)ticketTest;
/*
存钱、取钱演示
*/
- (void)moneyTest;
- (void)otherTest;
#pragma mark - 暴露给子类去使用
-(void)__saveMoney;
- (void)__drawMoney;
- (void)__saleTicket;
@end
BaseDemo.m文件
#import "BaseDemo.h"
@interface BaseDemo ()
@property (nonatomic,assign) int ticketsCount;
@property (nonatomic,assign) int money;
@end
@implementation BaseDemo
/*
存钱、取钱演示
*/
- (void)moneyTest{
self.money = 100;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<10; i++) {
[self __saveMoney];
}
});
dispatch_async(queue, ^{
for (int i=0; i<10; i++) {
[self __drawMoney];
}
});
}
/*
存钱
*/
-(void)__saveMoney{
int oldMoney = self.money;
sleep(.2);
oldMoney += 50;
self.money = oldMoney;
NSLog(@"存50,还剩 %d 元 - %@",oldMoney,[NSThread currentThread]);
}
/*
取钱
*/
- (void)__drawMoney{
int oldMoney = self.money;
sleep(.2);
oldMoney -= 20;
self.money = oldMoney;
NSLog(@"取20,还剩 %d 元 - %@",oldMoney,[NSThread currentThread]);
}
/*
卖1张票
*/
- (void)__saleTicket{
int oldTicketsCount = self.ticketsCount;
sleep(.2);//突出安全隐患问题
oldTicketsCount --;
self.ticketsCount = oldTicketsCount;
NSLog(@"还剩%d张票",oldTicketsCount);
}
/*
卖票演示
*/
- (void)ticketTest{
self.ticketsCount = 15;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self __saleTicket];
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self __saleTicket];
}
});
dispatch_async(queue, ^{
for (int i=0; i<5; i++) {
[self __saleTicket];
}
});
}
- (void)otherTest{
}
@end
OSSpinLock锁:
OSSpinLockDemo.h文件
#import "BaseDemo.h"
@interface OSSpinLockDemo : BaseDemo
@end
OSSpinLockDemo.m文件
#import "OSSpinLockDemo.h"
#import <libkern/OSAtomic.h>
@interface OSSpinLockDemo ()
@property (nonatomic,assign) OSSpinLock moneyLock;
@property (nonatomic,assign) OSSpinLock ticketLock;
@end
@implementation OSSpinLockDemo
- (instancetype)init{
if (self = [super init]) {
self.moneyLock = OS_SPINLOCK_INIT;
self.ticketLock = OS_SPINLOCK_INIT;
}
return self;
}
- (void)__drawMoney{
OSSpinLockLock(&_moneyLock);
[super __drawMoney];
OSSpinLockUnlock(&_moneyLock);
}
- (void)__saveMoney{
OSSpinLockLock(&_moneyLock);
[super __saveMoney];
OSSpinLockUnlock(&_moneyLock);
}
- (void)__saleTicket{
OSSpinLockLock(&_ticketLock);
[super __saleTicket];
OSSpinLockUnlock(&_ticketLock);
}
@end
os_unfair_lock锁:
OSUnfairLockDemo.h文件
#import "BaseDemo.h"
@interface OSUnfairLockDemo : BaseDemo
@end
OSUnfairLockDemo.m文件
#import "OSUnfairLockDemo.h"
#import <os/lock.h>
@interface OSUnfairLockDemo ()
@property (nonatomic,assign) os_unfair_lock moneyLock;
@property (nonatomic,assign) os_unfair_lock ticketLock;
@end
@implementation OSUnfairLockDemo
- (instancetype)init{
if (self = [super init]) {
self.moneyLock = OS_UNFAIR_LOCK_INIT;
self.ticketLock = OS_UNFAIR_LOCK_INIT;
}
return self;
}
- (void)__saleTicket{
os_unfair_lock_lock(&_ticketLock);
[super __saleTicket];
os_unfair_lock_unlock(&_ticketLock);
}
- (void)__saveMoney{
os_unfair_lock_lock(&_moneyLock);
[super __saveMoney];
os_unfair_lock_unlock(&_moneyLock);
}
- (void)__drawMoney{
os_unfair_lock_lock(&_moneyLock);
[super __drawMoney];
os_unfair_lock_unlock(&_moneyLock);
}
@end
pthread_mutex锁:
MutexDemo.h文件
#import "BaseDemo.h"
@interface MutexDemo : BaseDemo
@end
MutexDemo.m文件
#import "MutexDemo.h"
#import <pthread.h>
@interface MutexDemo ()
@property (nonatomic,assign) pthread_mutex_t ticketMutex;
@property (nonatomic,assign) pthread_mutex_t moneyMutex;
@end
@implementation MutexDemo
- (void)__initMutex:(pthread_mutex_t *)mutex{
//初始化属性
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
//初始化锁
pthread_mutex_init(mutex, &attr);
//销毁属性
pthread_mutexattr_destroy(&attr);
}
- (instancetype)init{
if (self = [super init]) {
[self __initMutex:&_ticketMutex];
[self __initMutex:&_moneyMutex];
}
return self;
}
- (void)__saleTicket{
pthread_mutex_lock(&_ticketMutex);
[super __saleTicket];
pthread_mutex_unlock(&_ticketMutex);
}
- (void)__saveMoney{
pthread_mutex_lock(&_moneyMutex);
[super __saveMoney];
pthread_mutex_unlock(&_moneyMutex);
}
- (void)__drawMoney{
pthread_mutex_lock(&_moneyMutex);
[super __drawMoney];
pthread_mutex_unlock(&_moneyMutex);
}
- (void)dealloc{
pthread_mutex_destroy(&_moneyMutex);
pthread_mutex_destroy(&_ticketMutex);
}
@end
pthread_mutex递归锁:
MutexDemo2.h文件
#import "BaseDemo.h"
@interface MutexDemo2 : BaseDemo
@end
MutexDemo2.m文件
#import "MutexDemo2.h"
#import <pthread.h>
@interface MutexDemo2 ()
@property (nonatomic,assign) pthread_mutex_t mutex;
@end
@implementation MutexDemo2
- (void)__initMutex:(pthread_mutex_t *)mutex{
//递归锁:允许同一个线程对这把锁进行重复加锁
//初始化属性
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);//递归锁
//初始化锁
pthread_mutex_init(mutex, &attr);
//销毁属性
pthread_mutexattr_destroy(&attr);
}
- (instancetype)init{
if (self = [super init]) {
[self __initMutex:&_mutex];
}
return self;
}
//递归锁
- (void)otherTest{
pthread_mutex_lock(&_mutex);
NSLog(@"%s",__func__);
static int count = 0;
if (count<10) {
count ++;
[self otherTest];
}
pthread_mutex_unlock(&_mutex);
}
- (void)dealloc{
pthread_mutex_destroy(&_mutex);
}
@end
pthread_mutex - 条件锁:
MutexDemo3.h文件
#import "BaseDemo.h"
@interface MutexDemo3 : BaseDemo
@end
MutexDemo3.m文件
#import "MutexDemo3.h"
#import <pthread.h>
@interface MutexDemo3 ()
@property (nonatomic,assign) pthread_mutex_t mutex;
@property (nonatomic,assign) pthread_cond_t cond;
@property (nonatomic,strong)NSMutableArray *data;
@end
@implementation MutexDemo3
- (instancetype)init{
if (self = [super init]) {
//初始化属性
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
//初始化锁
pthread_mutex_init(&_mutex, &attr);
//销毁属性
pthread_mutexattr_destroy(&attr);
//初始化条件
pthread_cond_init(&_cond, NULL);
self.data = [NSMutableArray array];
}
return self;
}
- (void)otherTest{
[[[NSThread alloc] initWithTarget:self selector:@selector(__remove) object:nil] start];
[[[NSThread alloc] initWithTarget:self selector:@selector(__add) object:nil] start];
}
//线程1
//删除数组中的元素
- (void)__remove{
pthread_mutex_lock(&_mutex);//加锁
if (self.data.count == 0) {
//等待
pthread_cond_wait(&_cond, &_mutex);//进入等待时,就解锁,条件唤醒时,再加锁
}
[self.data removeLastObject];
NSLog(@"删除了元素");
pthread_mutex_unlock(&_mutex);//解锁
}
//线程2
//往数组中添加元素
- (void)__add{
pthread_mutex_lock(&_mutex);
[self.data addObject:@"Test"];
NSLog(@"添加了元素");
//信号:唤醒一个线程
pthread_cond_signal(&_cond);
//广播:唤醒所有线程
pthread_cond_broadcast(&_cond);
pthread_mutex_unlock(&_mutex);
}
//条件锁流程:假设线程1先进来,就加一把锁,这时线程2也进来了,就进行等待;
线程1发现数组里没有元素,就进行等待,这时候会先放开这把锁,等待这个条件将来进行唤醒,暂时休眠;
这个时候线程2就加锁进入addObject方法,然后紧接着唤醒条件的操作,然后解锁,
这个时候线程1的等待那里就重新加锁,然后往下执行,再进行解锁。
- (void)dealloc{
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_cond);
}
@end
NSCondition:
LDNSCondition.h文件
#import "BaseDemo.h"
@interface LDNSCondition : BaseDemo
@end
LDNSCondition.m文件
#import "LDNSCondition.h"
@interface LDNSCondition ()
@property (nonatomic,strong) NSCondition *condition;
@property (nonatomic,strong)NSMutableArray *data;
@end
@implementation LDNSCondition
- (instancetype)init{
if (self = [super init]) {
self.condition = [[NSCondition alloc] init];
self.data = [NSMutableArray array];
}
return self;
}
- (void)otherTest{
[[[NSThread alloc] initWithTarget:self selector:@selector(__remove) object:nil] start];
[[[NSThread alloc] initWithTarget:self selector:@selector(__add) object:nil] start];
}
//线程1
//删除数组中的元素
- (void)__remove{
[self.condition lock];;//加锁
if (self.data.count == 0) {
//等待
[self.condition wait];
}
[self.data removeLastObject];
NSLog(@"删除了元素");
[self.condition unlock];//解锁
}
//线程2
//往数组中添加元素
- (void)__add{
[self.condition lock];
[self.data addObject:@"Test"];
NSLog(@"添加了元素");
//信号:唤醒一个线程
[self.condition signal];
[self.condition unlock];
}@end
NSConditionLock:
LDNSConditionLockDemo.h文件
#import "BaseDemo.h"
@interface LDNSConditionLockDemo : BaseDemo
@end
LDNSConditionLockDemo.m文件
#import "LDNSConditionLockDemo.h"
@interface LDNSConditionLockDemo ()
@property (nonatomic,strong) NSConditionLock *conditionLock;
@end
@implementation LDNSConditionLockDemo
- (instancetype)init{
if (self = [super init]) {
self.conditionLock = [[NSConditionLock alloc] initWithCondition:1];
}
return self;
}
- (void)otherTest{
[[[NSThread alloc] initWithTarget:self selector:@selector(__remove) object:nil] start];
[[[NSThread alloc] initWithTarget:self selector:@selector(__add) object:nil] start];
}
//线程1
//删除数组中的元素
- (void)__remove{
[self.conditionLock lockWhenCondition:1];;//加锁
NSLog(@"删除了元素");
[self.conditionLock unlockWithCondition:2];//解锁
}
//线程2
//往数组中添加元素
- (void)__add{
[self.conditionLock lockWhenCondition:2];
NSLog(@"添加了元素");
[self.conditionLock unlock];
}
@end
dispatch_queue(DISPATCH_QUEUE_SERIAL):
SerialQueueDemo.h文件
#import "BaseDemo.h"
@interface SerialQueueDemo : BaseDemo
@end
SerialQueueDemo.m文件
#import "SerialQueueDemo.h"
@interface SerialQueueDemo ()
@property (nonatomic,strong) dispatch_queue_t ticketQueue;
@property (nonatomic,strong) dispatch_queue_t moneyQueue;
@end
@implementation SerialQueueDemo
- (instancetype)init{
if (self = [super init]) {
self.ticketQueue = dispatch_queue_create("ticketQueue", DISPATCH_QUEUE_SERIAL);
self.moneyQueue = dispatch_queue_create("moneyQueue", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)__drawMoney{
dispatch_sync(self.moneyQueue, ^{
[super __drawMoney];
});
}
- (void)__saveMoney{
dispatch_sync(self.moneyQueue, ^{
[super __saveMoney];
});
}
- (void)__saleTicket{
dispatch_sync(self.ticketQueue, ^{
[super __saleTicket];
});
}
@end
dispatch_semaphore:
SemaphoreDemo.h文件
#import "BaseDemo.h"
@interface SemaphoreDemo : BaseDemo
@end
SemaphoreDemo.m文件
#import "SemaphoreDemo.h"
@interface SemaphoreDemo ()
@property (nonatomic,strong) dispatch_semaphore_t semaphore;
@property (nonatomic,strong) dispatch_semaphore_t ticketSemaphore;
@property (nonatomic,strong) dispatch_semaphore_t moneySemaphore;
@end
@implementation SemaphoreDemo
- (instancetype)init{
if (self = [super init]) {
self.semaphore = dispatch_semaphore_create(5);//最大并发数量是5
self.ticketSemaphore = dispatch_semaphore_create(1);//最大并发数量是5
self.moneySemaphore = dispatch_semaphore_create(1);//最大并发数量是5
}
return self;
}
- (void)otherTest{
for (int i=0; i<20; i++) {
[[[NSThread alloc] initWithTarget:self selector:@selector(test) object:nil] start];
}
}
//设置同一时间最大并发数量
- (void)test{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
//wait函数判断:如果信号量的值>0,就让信号量的值-1,然后继续往下执行代码,
//如果信号量的值<=0,就会休眠等待,直到信号量的值变成>0,然后就让信号量的值-1,然后继续往下执行代码,
sleep(2);
NSLog(@"test - %@",[NSThread currentThread]);
//让信号量的值+1
dispatch_semaphore_signal(self.semaphore);
}
- (void)__drawMoney{
dispatch_semaphore_wait(self.moneySemaphore, DISPATCH_TIME_FOREVER);
[super __drawMoney];
dispatch_semaphore_signal(self.moneySemaphore);
}
- (void)__saveMoney{
dispatch_semaphore_wait(self.moneySemaphore, DISPATCH_TIME_FOREVER);
[super __saveMoney];
dispatch_semaphore_signal(self.moneySemaphore);
}
- (void)__saleTicket{
dispatch_semaphore_wait(self.moneySemaphore, DISPATCH_TIME_FOREVER);
[super __saleTicket];
dispatch_semaphore_signal(self.moneySemaphore);
}
@end
@synchronized
SynchronizedDemo.h文件
#import "BaseDemo.h"
@interface SynchronizedDemo : BaseDemo
@end
SynchronizedDemo.m文件
#import "SynchronizedDemo.h"
@implementation SynchronizedDemo
- (void)__drawMoney{
@synchronized (self) {
[super __drawMoney];
}
}
- (void)__saveMoney{
@synchronized (self) {
[super __saveMoney];
}
}
- (void)__saleTicket{
static NSObject *lock;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
lock = [[NSObject alloc] init];
});
@synchronized (lock) {
[super __saleTicket];
}
}
@end
同步方案性能对比
-
性能从高到低排序
os_unfair_lock是iOS10以后才能用,OSSpinLock苹果不推荐使用,所以dispatch_semaphore和pthread_mutex两个使用频率比较高
自旋锁、互斥锁比较
- 什么情况使用自旋锁比较划算?
预计线程等待锁的时间很短
加锁的代码(临界区)经常被调用,但竞争情况很少发生
CPU资源不紧张
多核处理器 - 什么情况使用互斥锁比较划算
预计线程等待锁的时间较长
单核处理器
临界区有IO操作
临界区代码复杂或者循环量大
临界区竞争非常激烈
atomic
- atomic用于保证属性setter、getter的原子性操作,相当于在getter和setter内部加了线程同步的锁
- 可以参考源码objc4的objc-accessors.mm
- 它并不能保证使用属性的过程是线程安全的
nonatomic和atomic
atom:原子,不可再分割的单位
atomic:原子性
给属性加上atomic修饰,可以保证属性的setter和getter都是原子性操作,也就是保证setter和getter内部是线程同步的
atomic,iOS中使用频率很低,atomic太耗性能,一般在Mac中使用
iOS中的读写安全方案
IO操作,文件操作
- 从文件中读取内容
- 往文件中写入内容
思考:如何实现以下场景(多读单写的操作)
- 同一时间,只能有1个线程进行写的操作
- 同一时间,允许有多个线程进行读的操作
- 同一时间,不允许既有写的操作,又有读的操作
上面的场景就是典型的“多读单写”,经常用于文件等数据的读写操作,iOS中的实现方案有 - pthread_rwlock:读写锁
- dispatch_barrier_async:异步栅栏调用
pthread_rwlock
-
等待锁的线程进入休眠
代码如下:
#import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
@property (nonatomic,assign) pthread_rwlock_t lock;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化锁
pthread_rwlock_init(&_lock, NULL);
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i=0; i<10; i++) {
dispatch_async(queue, ^{
[self read];
});
dispatch_async(queue, ^{
[self write];
});
}
}
- (void)read{//允许多条线程读取东西,即多读单写操作
pthread_rwlock_rdlock(&_lock);
sleep(1);
NSLog(@"%s",__func__);
pthread_rwlock_unlock(&_lock);
}
- (void)write{
pthread_rwlock_wrlock(&_lock);
sleep(1);
NSLog(@"%s",__func__);
pthread_rwlock_unlock(&_lock);
}
- (void)dealloc{
pthread_rwlock_destroy(&_lock);
}
@end
dispatch_barrier_async
- 这个函数传入的并发队列必须是自己通过dispatch_queue_create创建的
-
如果传入的是一个串行或是一个全局的并发队列,那这个函数便等同于dispatch_async函数的效果
代码如下:
#import "ViewController.h"
#import <pthread.h>
@interface ViewController ()
@property (nonatomic,strong) dispatch_queue_t queue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT);
for (int i=0; i<10; i++) {
[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