多線程-GCD

iOS中的常見多線程方案
GCD的常用函數(shù)
  • GCD中有2個用來執(zhí)行任務(wù)的函數(shù)
    用同步的方式執(zhí)行任務(wù)
    dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);
    queue:隊列
    block:任務(wù)
    用異步的方式執(zhí)行任務(wù)
    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(@"執(zhí)行任務(wù) - %@",[NSThread currentThread]);
});
打印結(jié)果:執(zhí)行任務(wù) - <NSThread: 0x6000039accc0>{number = 4, name = (null)}
在子線程中打印
dispatch_sync(queue, ^{
        NSLog(@"執(zhí)行任務(wù) - %@",[NSThread currentThread]);
});
打印結(jié)果:執(zhí)行任務(wù) - <_NSMainThread: 0x600000370a80>{number = 1, name = main}
在當(dāng)前的線程中打印
GCD的隊列
  • GCD的隊列可以分為2大類型
    并發(fā)隊列(Concurrent Dispatch Queue):可以讓多個任務(wù)并發(fā)(同時)執(zhí)行(自動開啟多個線程同時執(zhí)行任務(wù));并發(fā)功能只有在異步(dispatch_async)函數(shù)下才有效
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
    for (int i=0; i<5; i++) {
       NSLog(@"執(zhí)行任務(wù)1 %d - %@",i,[NSThread currentThread]);
    }
});
dispatch_async(queue, ^{
    for (int i=0; i<5; i++) {
       NSLog(@"執(zhí)行任務(wù)2 %d - %@",i,[NSThread currentThread]);
    }
});
打印結(jié)果:
2023-04-12 19:52:08.865359+0800 inter_GCD[1789:28975] 執(zhí)行任務(wù)2 0 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.865389+0800 inter_GCD[1789:28972] 執(zhí)行任務(wù)1 0 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.865975+0800 inter_GCD[1789:28972] 執(zhí)行任務(wù)1 1 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.865976+0800 inter_GCD[1789:28975] 執(zhí)行任務(wù)2 1 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866111+0800 inter_GCD[1789:28975] 執(zhí)行任務(wù)2 2 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866071+0800 inter_GCD[1789:28972] 執(zhí)行任務(wù)1 2 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.866169+0800 inter_GCD[1789:28975] 執(zhí)行任務(wù)2 3 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866218+0800 inter_GCD[1789:28975] 執(zhí)行任務(wù)2 4 - <NSThread: 0x6000011e8140>{number = 5, name = (null)}
2023-04-12 19:52:08.866328+0800 inter_GCD[1789:28972] 執(zhí)行任務(wù)1 3 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}
2023-04-12 19:52:08.866395+0800 inter_GCD[1789:28972] 執(zhí)行任務(wù)1 4 - <NSThread: 0x6000011e4c00>{number = 6, name = (null)}

串行隊列(Serial Dispatch Queue):讓任務(wù)一個接著一個地執(zhí)行(一個任務(wù)執(zhí)行完畢后,再執(zhí)行下一個任務(wù))

dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
   for (int i=0; i<5; i++) {
       NSLog(@"執(zhí)行任務(wù)1 %d - %@",i,[NSThread currentThread]);
   }
    });
dispatch_async(queue, ^{
    for (int i=0; i<5; i++) {
       NSLog(@"執(zhí)行任務(wù)2 %d - %@",i,[NSThread currentThread]);
    }
});
打印結(jié)果:
2023-04-12 20:01:32.158700+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)1 0 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159041+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)1 1 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159110+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)1 2 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159444+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)1 3 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159508+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)1 4 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159644+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)2 0 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159719+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)2 1 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159772+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)2 2 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159836+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)2 3 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
2023-04-12 20:01:32.159892+0800 inter_GCD[1984:39989] 執(zhí)行任務(wù)2 4 - <NSThread: 0x600001dd8480>{number = 5, name = (null)}
容易混淆的術(shù)語
  • 有4個術(shù)語比較容易混淆:同步、異步、并發(fā)、串行
  • 同步和異步主要影響:能不能開啟新的線程
    同步:在當(dāng)前線程中執(zhí)行任務(wù),不具備開啟新線程的能力
    異步:在新的線程中執(zhí)行任務(wù),具備開啟新線程的能力
  • 并發(fā)和串行主要影響:任務(wù)的執(zhí)行方式
    并發(fā):多個任務(wù)并發(fā)(同時)執(zhí)行
    串行:一個任務(wù)執(zhí)行完畢后,再執(zhí)行下一個任務(wù)
    dispatch_sync和dispatch_async用來控制是否要開啟新的線程
    隊列的類型,決定了任務(wù)的執(zhí)行方式(并發(fā)、串行):并發(fā)隊列、串行隊列、主隊列(也是一種串行隊列)
各種隊列的執(zhí)行效果
  • 使用sync函數(shù)往當(dāng)前串行隊列中添加任務(wù),會卡住當(dāng)前的串行隊列(產(chǎn)生死鎖),代碼如下:
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"執(zhí)行任務(wù)1");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@"執(zhí)行任務(wù)2");
    });
    
    NSLog(@"執(zhí)行任務(wù)3");
}
打印結(jié)果:
2023-04-12 20:24:21.543663+0800 inter_GCD[2395:64891] 執(zhí)行任務(wù)1
(lldb) 崩掉原因:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

問題1:以上代碼是在主線程執(zhí)行的,會不會產(chǎn)生死鎖?
答:會產(chǎn)生死鎖。隊列的特點:排隊,F(xiàn)IFO(First In First Out),先進先出,所以當(dāng)前主隊列中正在執(zhí)行的任務(wù)是viewDidLoad這個方法,那么^{ NSLog(@"執(zhí)行任務(wù)2"); })任務(wù)應(yīng)該排在viewDidLoad方法之后,也就是執(zhí)行完NSLog(@"執(zhí)行任務(wù)3");代碼,而dispatch_sync:立馬在當(dāng)前線程執(zhí)行任務(wù),執(zhí)行完畢才能繼續(xù)往下執(zhí)行NSLog(@"執(zhí)行任務(wù)3");,所以就造成了死鎖。

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"執(zhí)行任務(wù)1");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@"執(zhí)行任務(wù)2");
    });
    NSLog(@"執(zhí)行任務(wù)3");
}
打印結(jié)果:
2023-04-12 20:40:46.578575+0800 inter_GCD[2631:81899] 執(zhí)行任務(wù)1
2023-04-12 20:40:46.578792+0800 inter_GCD[2631:81899] 執(zhí)行任務(wù)3
2023-04-12 20:40:46.664453+0800 inter_GCD[2631:81899] 執(zhí)行任務(wù)2

問題2:以上代碼是在主線程執(zhí)行的,會不會產(chǎn)生死鎖?
答:不會產(chǎn)生死鎖。
dispatch_sync:立馬在當(dāng)前線程同步執(zhí)行任務(wù)
dispatch_async:不要求立馬在當(dāng)前線程同步執(zhí)行任務(wù)

會死鎖
- (void)interview01{
    NSLog(@"執(zhí)行任務(wù)1");

    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        NSLog(@"執(zhí)行任務(wù)2");
        dispatch_sync(queue, ^{
            NSLog(@"執(zhí)行任務(wù)3");
        });
        NSLog(@"執(zhí)行任務(wù)4");
    });
    NSLog(@"執(zhí)行任務(wù)5");
}
不會死鎖
- (void)interview01{
    NSLog(@"執(zhí)行任務(wù)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(@"執(zhí)行任務(wù)2");
        dispatch_sync(queue2, ^{
            NSLog(@"執(zhí)行任務(wù)3");
        });
        NSLog(@"執(zhí)行任務(wù)4");
    });
    
    NSLog(@"執(zhí)行任務(wù)5");
}
不會死鎖
- (void)interview01{
    NSLog(@"執(zhí)行任務(wù)1");

    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        NSLog(@"執(zhí)行任務(wù)2");
        dispatch_sync(queue, ^{
            NSLog(@"執(zhí)行任務(wù)3");
        });
        NSLog(@"執(zhí)行任務(wù)4");
    });
    
    NSLog(@"執(zhí)行任務(wù)5");
}
dispatch_queue_t queue0 = dispatch_get_main_queue();//主隊列
dispatch_queue_t queue1 = dispatch_get_global_queue(0, 0);//全局并發(fā)隊列
dispatch_queue_t queue2 = dispatch_queue_create("queue3", DISPATCH_QUEUE_CONCURRENT);//手動創(chuàng)建并發(fā)隊列
dispatch_queue_t queue3 = dispatch_queue_create("queue4", DISPATCH_QUEUE_SERIAL);//手動創(chuàng)建串行隊列

下面代碼的打印結(jié)果是什么?

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");
}
打印結(jié)果:
1  3

[self performSelector:@selector(test) withObject:nil afterDelay:.0];這句代碼的本質(zhì)是:往RunLoop里邊添加了一個定時器。把當(dāng)前代碼放在主線程中可以走test里邊的代碼,因為主線程中默認開啟RunLoop,而當(dāng)前是在子線程中,沒有默認開啟的RunLoop,所以定時器是沒法工作的,這句代碼在子線程中沒有開啟RunLoop時是不走的,如果想實現(xiàn)[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");
}
打印結(jié)果:
1  3  2
[self performSelector:(SEL) withObject:(id)]
[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]
如上兩句代碼的本質(zhì)是不一樣的,[self performSelector:(SEL) withObject:(id)]本質(zhì)就是objc_msgSend(self,SEL),[self performSelector:(SEL) withObject:(id) afterDelay:(NSTimeInterval)]是和RunLoop有關(guān)的
GNUstep
  • GNUstep是GNU計劃的項目之一,它將Cocoa的OC庫重新開源實現(xiàn)了一遍
  • GNU源碼地址
  • 雖然GNUstep不是蘋果官方源碼,但還是具有一定的參考價值

下面代碼打印的結(jié)果是什么?

- (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");
}
打印結(jié)果:
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];代碼執(zhí)行完就去子線程中執(zhí)行^{
        NSLog(@"1");
    },執(zhí)行完畢子線程就退出了,下一句代碼又要在thread子線程中調(diào)用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");
}
打印結(jié)果:
1  2
隊列組的使用

思考:如何用GCD實現(xiàn)以下功能?

  • 異步并發(fā)執(zhí)行任務(wù)1、任務(wù)2
  • 等任務(wù)1、任務(wù)2都執(zhí)行完畢后,再回到主線程執(zhí)行任務(wù)3
- (void)viewDidLoad {
    [super viewDidLoad];
    //創(chuàng)建隊列組
    dispatch_group_t group = dispatch_group_create();
    //創(chuàng)建并發(fā)隊列
    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    //添加異步任務(wù)
    dispatch_group_async(group, queue, ^{
        for (int i=0; i<5; i++) {
            NSLog(@"任務(wù)1 - %@",[NSThread currentThread]);
        }
    });
    dispatch_group_async(group, queue, ^{
        for (int i=0; i<5; i++) {
            NSLog(@"任務(wù)2 - %@",[NSThread currentThread]);
        }
    });
    //等前面的任務(wù)執(zhí)行完畢后,會自動執(zhí)行這個任務(wù)
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            for (int i=0; i<5; i++) {
                NSLog(@"任務(wù)3 - %@",[NSThread currentThread]);
            }
        });
    });
    
}
多線程的安全隱患
  • 資源共享:1塊資源可能會被多個線程共享,也就是多個線程可能會訪問同一塊資源;比如多個線程訪問同一個對象、同一個變量、同一個文件
  • 當(dāng)多個線程訪問同一塊資源時,很容易引發(fā)數(shù)據(jù)錯亂和數(shù)據(jù)安全問題
多線程安全隱患示例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]);
}
打印結(jié)果:
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];
        }
    });
}
打印結(jié)果:
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張票
多線程安全隱患分析
多線程安全隱患的解決方案
  • 解決方案:使用線程同步技術(shù)(同步,就是協(xié)同步調(diào),按預(yù)定的先后次序進行運行)
  • 常用的線程同步技術(shù):加鎖


iOS中的線程同步方案
  • OSSpinLock
  • os_unfair_lock
  • pthread_mutex
  • dispatch_semaphore
  • dispatch_queue(DISPATCH_QUEUE_SERIAL)
  • NSLock
  • NSRecursiveLock
  • NSConditionLock
  • @synchronized
OSSpinLock
  • OSSpinLock叫做“自旋鎖”,等待鎖的線程會處于忙等(busy-wait)狀態(tài),一直占用著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目前已經(jīng)不再安全,可能會出現(xiàn)優(yōu)先級反轉(zhuǎn)問題:如果等待鎖的線程優(yōu)先級較高,它會一直占用著CPU資源,優(yōu)先級低的線程就無法釋放鎖.(iOS10.0之后蘋果不推薦使用)
  • 需要導(dǎo)入頭文件#import <libkern/OSAtomic.h>
    OSSpinLock常見函數(shù)用法:
//初始化
OSSpinLock lock = OS_SPINLOCK_INIT;
//嘗試加鎖(如果需要等待就不加鎖,直接返回false;如果不需要等待就加鎖,返回true)
bool result = OSSpinLockTry(&lock);
//加鎖
OSSpinLockLock(& lock);
解鎖
OSSpinLockUnlock(& lock);
os_unfair_lock
  • os_unfair_lock用于取代不安全的OSSpinLock,從iOS10開始才支持
  • 從底層調(diào)用看,等待os_unfair_lock鎖的線程會處于休眠狀態(tài),并非忙等
  • 需要導(dǎo)入頭文件#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叫做“互斥鎖”,等待鎖的線程會處于休眠狀態(tài)
  • 需要導(dǎo)入頭文件#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);
//銷毀相關(guān)資源
pthread_mutexattr_destroy(&attr);
pthread_mutex_destroy(&mutex);
  • pthread_mutex - 條件
    條件鎖應(yīng)用場景,線程1需要等待線程2執(zhí)行完畢再去執(zhí)行線程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的進一步封裝,可以設(shè)置具體的條件值


  • 能夠設(shè)置線程之間的依賴
  • 如果不設(shè)置condition,那么默認為0.
dispatch_queue(DISPATCH_QUEUE_SERIAL)
  • 直接使用GCD的串行隊列,也是可以實現(xiàn)線程同步的
dispatch_semaphore
  • semaphore叫做“信號量”
  • 信號量的初始值,可以用來控制線程并發(fā)訪問的最大數(shù)量
  • 信號量的初始值為1,代表同時只允許1條線程訪問資源,保證線程同步


@synchronized
  • @synchronized是對mutex遞歸鎖的封裝
  • 源碼查看:objc4中的objc-sync.mm文件
  • @synchronized(obj)內(nèi)部會生成obj對應(yīng)的遞歸鎖,然后進行加鎖、解鎖操作


把幾種加鎖代碼寫到一起進行比較:
先寫一個賣票、存錢、取錢的基類,然后再寫幾種分別加鎖的類

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{
    //遞歸鎖:允許同一個線程對這把鎖進行重復(fù)加鎖
    //初始化屬性
    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
//刪除數(shù)組中的元素
- (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
//往數(shù)組中添加元素
- (void)__add{
    pthread_mutex_lock(&_mutex);
    [self.data addObject:@"Test"];
    NSLog(@"添加了元素");
    //信號:喚醒一個線程
    pthread_cond_signal(&_cond);
    //廣播:喚醒所有線程
    pthread_cond_broadcast(&_cond);
    pthread_mutex_unlock(&_mutex);
}
//條件鎖流程:假設(shè)線程1先進來,就加一把鎖,這時線程2也進來了,就進行等待;
線程1發(fā)現(xiàn)數(shù)組里沒有元素,就進行等待,這時候會先放開這把鎖,等待這個條件將來進行喚醒,暫時休眠;
這個時候線程2就加鎖進入addObject方法,然后緊接著喚醒條件的操作,然后解鎖,
這個時候線程1的等待那里就重新加鎖,然后往下執(zhí)行,再進行解鎖。
- (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
//刪除數(shù)組中的元素
- (void)__remove{
    [self.condition lock];;//加鎖
    if (self.data.count == 0) {
        //等待
        [self.condition wait];
    }
    [self.data removeLastObject];
    NSLog(@"刪除了元素");
    [self.condition unlock];//解鎖
}
//線程2
//往數(shù)組中添加元素
- (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
//刪除數(shù)組中的元素
- (void)__remove{
    [self.conditionLock lockWhenCondition:1];;//加鎖
    NSLog(@"刪除了元素");
    [self.conditionLock unlockWithCondition:2];//解鎖
}
//線程2
//往數(shù)組中添加元素
- (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);//最大并發(fā)數(shù)量是5
        self.ticketSemaphore = dispatch_semaphore_create(1);//最大并發(fā)數(shù)量是5
        self.moneySemaphore = dispatch_semaphore_create(1);//最大并發(fā)數(shù)量是5
        
    }
    return self;
}
- (void)otherTest{
    for (int i=0; i<20; i++) {
        [[[NSThread alloc] initWithTarget:self selector:@selector(test) object:nil] start];
    }
}
//設(shè)置同一時間最大并發(fā)數(shù)量
- (void)test{
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
    //wait函數(shù)判斷:如果信號量的值>0,就讓信號量的值-1,然后繼續(xù)往下執(zhí)行代碼,
    //如果信號量的值<=0,就會休眠等待,直到信號量的值變成>0,然后就讓信號量的值-1,然后繼續(xù)往下執(zhí)行代碼,
    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兩個使用頻率比較高

自旋鎖、互斥鎖比較
  • 什么情況使用自旋鎖比較劃算?
    預(yù)計線程等待鎖的時間很短
    加鎖的代碼(臨界區(qū))經(jīng)常被調(diào)用,但競爭情況很少發(fā)生
    CPU資源不緊張
    多核處理器
  • 什么情況使用互斥鎖比較劃算
    預(yù)計線程等待鎖的時間較長
    單核處理器
    臨界區(qū)有IO操作
    臨界區(qū)代碼復(fù)雜或者循環(huán)量大
    臨界區(qū)競爭非常激烈
atomic
  • atomic用于保證屬性setter、getter的原子性操作,相當(dāng)于在getter和setter內(nèi)部加了線程同步的鎖
  • 可以參考源碼objc4的objc-accessors.mm
  • 它并不能保證使用屬性的過程是線程安全的

nonatomic和atomic
atom:原子,不可再分割的單位
atomic:原子性
給屬性加上atomic修飾,可以保證屬性的setter和getter都是原子性操作,也就是保證setter和getter內(nèi)部是線程同步的

atomic,iOS中使用頻率很低,atomic太耗性能,一般在Mac中使用

iOS中的讀寫安全方案

IO操作,文件操作

  • 從文件中讀取內(nèi)容
  • 往文件中寫入內(nèi)容

思考:如何實現(xiàn)以下場景(多讀單寫的操作)

  • 同一時間,只能有1個線程進行寫的操作
  • 同一時間,允許有多個線程進行讀的操作
  • 同一時間,不允許既有寫的操作,又有讀的操作
    上面的場景就是典型的“多讀單寫”,經(jīng)常用于文件等數(shù)據(jù)的讀寫操作,iOS中的實現(xiàn)方案有
  • pthread_rwlock:讀寫鎖
  • dispatch_barrier_async:異步柵欄調(diào)用
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
  • 這個函數(shù)傳入的并發(fā)隊列必須是自己通過dispatch_queue_create創(chuàng)建的
  • 如果傳入的是一個串行或是一個全局的并發(fā)隊列,那這個函數(shù)便等同于dispatch_async函數(shù)的效果




    代碼如下:

#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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 查看oc文件底層結(jié)構(gòu) 支持ARC、指定運行時系統(tǒng)版本 一、iOS中常見的多線程方案 二、容易混淆的術(shù)語 有4種術(shù)語...
    節(jié)奏l(xiāng)hl閱讀 946評論 0 2
  • 線程 進程和線程 線程是進程的基本執(zhí)行單元,一個進程的所有任務(wù)都在線程中執(zhí)行。 進程要想執(zhí)行任務(wù),必須得有線程,進...
    MrQun閱讀 473評論 0 0
  • 本文內(nèi)容任務(wù)、隊列的概念、創(chuàng)建方式任務(wù) + 隊列的6種組合的執(zhí)行方式線程間如何通信dispatch_once、di...
    小秀秀耶閱讀 1,080評論 0 9
  • 轉(zhuǎn)載于作者:行走少年郎 原地址 鏈接:http://www.itdecent.cn/p/2d57c72016c6...
    WSWshallwe閱讀 314評論 0 0
  • 目錄 一、基本概念1.多線程2.串行和并行, 并發(fā)3.隊列與任務(wù)4.同步與異步5.線程狀態(tài)6.多線程方案 二、GC...
    BohrIsLay閱讀 1,702評論 5 12

友情鏈接更多精彩內(nèi)容