参考链接
iOS多线程
iOS 多线程:『GCD』详尽总结
iOS简单优雅的实现复杂情况下的串行需求(各种锁、GCD 、NSOperationQueue...)
GCD组合结果
区别 | 并发队列 | 串行队列 | 主队列 |
---|---|---|---|
同步(sync) | 没有开启新线程,串行执行任务 | 没有开启新线程,串行执行任务 | 主线程调用:死锁卡住不执行。其他线程调用:没有开启新线程,串行执行任务 |
异步(async) | 有开启新线程,并发执行任务 | 有开启新线程(1条),串行执行任务 | 没有开启新线程,串行执行任务 |
应用
顺序网络请求
互斥锁实现
#import <pthread.h>
/**
pthread_mutex 互斥锁
*/
-(void)usePthred{
static pthread_mutex_t pLock;
pthread_mutex_init(&pLock, NULL);
pthread_mutex_lock(&pLock);
NSLog(@"1上锁");
[self doSomeThingForFlag:1 finish:^{
NSLog(@"1解锁");
pthread_mutex_unlock(&pLock);
}];
pthread_mutex_lock(&pLock);
NSLog(@"2上锁");
[self doSomeThingForFlag:2 finish:^{
NSLog(@"2解锁");
pthread_mutex_unlock(&pLock);
}];
pthread_mutex_lock(&pLock);
NSLog(@"3上锁");
[self doSomeThingForFlag:3 finish:^{
NSLog(@"3解锁");
pthread_mutex_unlock(&pLock);
}];
pthread_mutex_lock(&pLock);
NSLog(@"4上锁");
[self doSomeThingForFlag:4 finish:^{
NSLog(@"4解锁");
pthread_mutex_unlock(&pLock);
}];
}
---------------------
作者:spicyShrimp
来源:CSDN
原文:https://blog.csdn.net/spicyshrimp/article/details/70845822
版权声明:本文为博主原创文章,转载请附上博文链接!
OSSpinLock 自旋锁
#import <libkern/OSAtomic.h>
/**
OSSpinLock 自旋锁
*/
-(void)useOSSpinLock{
__block OSSpinLock oslock = OS_SPINLOCK_INIT;
OSSpinLockLock(&oslock);
NSLog(@"1上锁");
[self doSomeThingForFlag:1 finish:^{
NSLog(@"1解锁");
OSSpinLockUnlock(&oslock);
}];
OSSpinLockLock(&oslock);
NSLog(@"2上锁");
[self doSomeThingForFlag:2 finish:^{
NSLog(@"2解锁");
OSSpinLockUnlock(&oslock);
}];
OSSpinLockLock(&oslock);
NSLog(@"3上锁");
[self doSomeThingForFlag:3 finish:^{
NSLog(@"3解锁");
OSSpinLockUnlock(&oslock);
}];
OSSpinLockLock(&oslock);
NSLog(@"4上锁");
[self doSomeThingForFlag:4 finish:^{
NSLog(@"4解锁");
OSSpinLockUnlock(&oslock);
}];
}
---------------------
作者:spicyShrimp
来源:CSDN
原文:https://blog.csdn.net/spicyshrimp/article/details/70845822
版权声明:本文为博主原创文章,转载请附上博文链接!
GCD实现信号量实现
/**
GCD single
*/
-(void)useGCDSingle{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"1阻塞线程");
[self doSomeThingForFlag:1 finish:^{
NSLog(@"1释放线程");
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"2阻塞线程");
[self doSomeThingForFlag:2 finish:^{
NSLog(@"2释放线程");
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"3阻塞线程");
[self doSomeThingForFlag:3 finish:^ {
NSLog(@"3释放线程");
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"4阻塞线程");
[self doSomeThingForFlag:4 finish:^{
NSLog(@"4释放线程");
dispatch_semaphore_signal(semaphore);
}];
}
---------------------
作者:spicyShrimp
来源:CSDN
原文:https://blog.csdn.net/spicyshrimp/article/details/70845822
版权声明:本文为博主原创文章,转载请附上博文链接!
GCD队列的暂停和恢复
/**
GCD队列的暂停和恢复
*/
-(void)useGCDSuspendAndResume{
//串行队列
dispatch_queue_t myqueue = dispatch_queue_create("com.charles.queue", NULL);
dispatch_async(myqueue, ^{
dispatch_suspend(myqueue);
[self doSomeThingForFlag:1 finish:^(NSInteger flag) {
dispatch_resume(myqueue);
}];
});
dispatch_async(myqueue, ^{
dispatch_suspend(myqueue);
[self doSomeThingForFlag:2 finish:^(NSInteger flag) {
dispatch_resume(myqueue);
}];
});
dispatch_async(myqueue, ^{
dispatch_suspend(myqueue);
[self doSomeThingForFlag:3 finish:^(NSInteger flag) {
dispatch_resume(myqueue);
}];
});
dispatch_async(myqueue, ^{
dispatch_suspend(myqueue);
[self doSomeThingForFlag:4 finish:^(NSInteger flag) {
dispatch_resume(myqueue);
}];
});
}
---------------------
作者:spicyShrimp
来源:CSDN
原文:https://blog.csdn.net/spicyshrimp/article/details/70845822
版权声明:本文为博主原创文章,转载请附上博文链接!
NSOperationQueue
/**
operationQueue的暂停和恢复
*/
-(void)useOperationQueue{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
__weak typeof(self)weakSelf = self;
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
[queue setSuspended:YES];
[weakSelf doSomeThingForFlag:1 finish:^(NSInteger flag) {
[queue setSuspended:NO];
}];
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
[queue setSuspended:YES];
[weakSelf doSomeThingForFlag:2 finish:^(NSInteger flag) {
[queue setSuspended:NO];
}];
}];
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
[queue setSuspended:YES];
[weakSelf doSomeThingForFlag:3 finish:^(NSInteger flag) {
[queue setSuspended:NO];
}];
}];
NSBlockOperation * operation4 = [NSBlockOperation blockOperationWithBlock:^{
[queue setSuspended:YES];
[weakSelf doSomeThingForFlag:4 finish:^(NSInteger flag) {
[queue setSuspended:NO];
}];
}];
[operation4 addDependency:operation3];
[operation3 addDependency:operation2];
[operation2 addDependency:operation1];
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];
[queue addOperation:operation4];
}
---------------------
作者:spicyShrimp
来源:CSDN
原文:https://blog.csdn.net/spicyshrimp/article/details/70845822
版权声明:本文为博主原创文章,转载请附上博文链接!