信号量有三个方法可用://指定初始信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
//使信号量的计数+1
dispatch_semaphore_signal(semaphore);
//等待,当信号量大于0时,会执行后续的代码,同时信号量-1
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
下面模拟下多线程下资源争抢的问题dispatch_queue_t queue = dispatch_queue_create("dsgsdg", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 100; i++) {
dispatch_async(queue, ^{
NSLog(@"%d", i);
});
}
打印结果如下2018-07-14 23:35:45.485043+0800 semphore[6574:322084] 1
2018-07-14 23:35:45.485043+0800 semphore[6574:322082] 2
2018-07-14 23:35:45.485043+0800 semphore[6574:322083] 0
2018-07-14 23:35:45.485043+0800 semphore[6574:322085] 3
2018-07-14 23:35:45.485102+0800 semphore[6574:322109] 4
2018-07-14 23:35:45.485137+0800 semphore[6574:322110] 5
2018-07-14 23:35:45.485168+0800 semphore[6574:322111] 6
2018-07-14 23:35:45.485208+0800 semphore[6574:322112] 7
2018-07-14 23:35:45.485238+0800 semphore[6574:322113] 8
2018-07-14 23:35:45.485267+0800 semphore[6574:322114] 9
2018-07-14 23:35:45.485276+0800 semphore[6574:322084] 10
2018-07-14 23:35:45.485285+0800 semphore[6574:322082] 12
2018-07-14 23:35:45.485296+0800 semphore[6574:322083] 13
2018-07-14 23:35:45.485313+0800 semphore[6574:322115] 11
2018-07-14 23:35:45.485338+0800 semphore[6574:322116] 14
2018-07-14 23:35:45.485389+0800 semphore[6574:322117] 15
2018-07-14 23:35:45.485396+0800 semphore[6574:322085] 16
2018-07-14 23:35:45.485433+0800 semphore[6574:322109] 18
2018-07-14 23:35:45.485430+0800 semphore[6574:322118] 17
2018-07-14 23:35:45.485468+0800 semphore[6574:322119] 19
打印错乱了,我们可以使用信号量来使线程同步,使各个线程按先后顺序打印//指定初始信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t queue = dispatch_queue_create("dsgsdg", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 100; i++) {
//等待,当信号量大于0时,会执行后续的代码,同时信号量-1
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(queue, ^{
NSLog(@"%d", i);
//使信号量的计数+1
dispatch_semaphore_signal(semaphore);
});
}
打印结果如下2018-07-14 23:38:53.083162+0800 semphore[6615:324602] 0
2018-07-14 23:38:53.083339+0800 semphore[6615:324602] 1
2018-07-14 23:38:53.083490+0800 semphore[6615:324602] 2
2018-07-14 23:38:53.083628+0800 semphore[6615:324602] 3
2018-07-14 23:38:53.083767+0800 semphore[6615:324602] 4
2018-07-14 23:38:53.083871+0800 semphore[6615:324602] 5
2018-07-14 23:38:53.083971+0800 semphore[6615:324602] 6
2018-07-14 23:38:53.084091+0800 semphore[6615:324602] 7
2018-07-14 23:38:53.084206+0800 semphore[6615:324602] 8
2018-07-14 23:38:53.084328+0800 semphore[6615:324602] 9
2018-07-14 23:38:53.084455+0800 semphore[6615:324602] 10
2018-07-14 23:38:53.084573+0800 semphore[6615:324602] 11
2018-07-14 23:38:53.084679+0800 semphore[6615:324602] 12
2018-07-14 23:38:53.084802+0800 semphore[6615:324602] 13
2018-07-14 23:38:53.084907+0800 semphore[6615:324602] 14
2018-07-14 23:38:53.085012+0800 semphore[6615:324602] 15
2018-07-14 23:38:53.085135+0800 semphore[6615:324602] 16
2018-07-14 23:38:53.085268+0800 semphore[6615:324602] 17
2018-07-14 23:38:53.085372+0800 semphore[6615:324602] 18
2018-07-14 23:38:53.085485+0800 semphore[6615:324602] 19
2018-07-14 23:38:53.111002+0800 semphore[6615:324602] 20
2018-07-14 23:38:53.111182+0800 semphore[6615:324602] 21
2018-07-14 23:38:53.111327+0800 semphore[6615:324602] 22
2018-07-14 23:38:53.111457+0800 semphore[6615:324602] 23
2018-07-14 23:38:53.111621+0800 semphore[6615:324602] 24
2018-07-14 23:38:53.111782+0800 semphore[6615:324602] 25
2018-07-14 23:38:53.111925+0800 semphore[6615:324602] 26
2018-07-14 23:38:53.112060+0800 semphore[6615:324602] 27
2018-07-14 23:38:53.112207+0800 semphore[6615:324602] 28
2018-07-14 23:38:53.112403+0800 semphore[6615:324602] 29
2018-07-14 23:38:53.112547+0800 semphore[6615:324602] 30
2018-07-14 23:38:53.112698+0800 semphore[6615:324602] 31
2018-07-14 23:38:53.112835+0800 semphore[6615:324602] 32
2018-07-14 23:38:53.112989+0800 semphore[6615:324602] 33
2018-07-14 23:38:53.113138+0800 semphore[6615:324602] 34
2018-07-14 23:38:53.113288+0800 semphore[6615:324602] 35
2018-07-14 23:38:53.113430+0800 semphore[6615:324602] 36
2018-07-14 23:38:53.113661+0800 semphore[6615:324602] 37
2018-07-14 23:38:53.113847+0800 semphore[6615:324602] 38
2018-07-14 23:38:53.114074+0800 semphore[6615:324602] 39
2018-07-14 23:38:53.114298+0800 semphore[6615:324602] 40
2018-07-14 23:38:53.114509+0800 semphore[6615:324602] 41
2018-07-14 23:38:53.114710+0800 semphore[6615:324602] 42
2018-07-14 23:38:53.114955+0800 semphore[6615:324602] 43
2018-07-14 23:38:53.115175+0800 semphore[6615:324602] 44
2018-07-14 23:38:53.115378+0800 semphore[6615:324602] 45
2018-07-14 23:38:53.115627+0800 semphore[6615:324602] 46
2018-07-14 23:38:53.115900+0800 semphore[6615:324602] 47
2018-07-14 23:38:53.116269+0800 semphore[6615:324602] 48