当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS GCD多线程

iOS GCD多线程

2018年11月25日  | 移动技术网移动技术  | 我要评论

本篇博客共分以下几个模块来介绍gcd的相关内容:

多线程相关概念多线程技术的优缺点比较?gcd中的三种队列类型the main queue(主线程串行队列)global queue(全局并发队列)custom queue (自定义队列)group queue (队列组)gcd中一些提供的常用dispatch方法

多线程相关概念

进程与线程 进程概念: 进程是程序在计算机上的一次执行活动,打开一个app,就开启了一个进程,可包含多个线程。线程概念: 独立执行的代码段,一个线程同时间只能执行一个任务,反之多线程并发就可以在同一时间执行多个任务。ios程序中,主线程(又叫作ui线程)主要任务是处理ui事件,显示和刷新ui,(只有主线程有直接修改ui的能力)耗时的操作放在子线程(又叫作后台线程、异步线程)。在ios中开子线程去处理耗时的操作,可以有效提高程序的执行效率,提高资源利用率。但是开启线程会占用一定的内存,(主线程的堆栈大小是1m,第二个线程开始都是512kb,并且该值不能通过编译器开关或线程api函数来更改)降低程序的性能。所以一般不要同时开很多线程。 线程相关 同步线程:同步线程会阻塞当前线程去执行线程内的任务,执行完之后才会反回当前线程。异步线程:异步线程不会阻塞当前线程,会开启其他线程去执行线程内的任务。串行队列:线程任务按先后顺序逐个执行(需要等待队列里面前面的任务执行完之后再执行新的任务)。并发队列:多个任务按添加顺序一起开始执行(不用等待前面的任务执行完再执行新的任务),但是添加间隔往往忽略不计,所以看着像是一起执行的。并发vs并行:并行是基于多核设备的,并行一定是并发,并发不一定是并行。 多线程中会出现的问题 critical section(临界代码段)
指的是不能同时被两个线程访问的代码段,比如一个变量,被并发进程访问后可能会改变变量值,造成数据污染(数据共享问题)。race condition (竞态条件)
当多个线程同时访问共享的数据时,会发生争用情形,第一个线程读取改变了一个变量的值,第二个线程也读取改变了这个变量的值,两个线程同时操作了该变量,此时他们会发生竞争来看哪个线程会最后写入这个变量,最后被写入的值将会被保留下来。deadlock (死锁)
两个(多个)线程都要等待对方完成某个操作才能进行下一步,这时就会发生死锁。thread safe(线程安全)
一段线程安全的代码(对象),可以同时被多个线程或并发的任务调度,不会产生问题,非线程安全的只能按次序被访问。所有mutable对象都是非线程安全的,所有immutable对象都是线程安全的,使用mutable对象,一定要用同步锁来同步访问(@synchronized)。互斥锁:能够防止多线程抢夺造成的数据安全问题,但是需要消耗大量的资源原子属性(atomic)加锁 atomic: 原子属性,为setter方法加锁,将属性以atomic的形式来声明,该属性变量就能支持互斥锁了。nonatomic: 非原子属性,不会为setter方法加锁,声明为该属性的变量,客户端应尽量避免多线程争夺同一资源。 context switch (上下文切换)
当一个进程中有多个线程来回切换时,context switch用来记录执行状态,这样的进程和一般的多线程进程没有太大差别,但会产生一些额外的开销。

多线程编程技术的优缺点比较

nsthread (抽象层次:低) 优点:轻量级,简单易用,可以直接操作线程对象缺点: 需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销。 cocoa nsoperation (抽象层次:中) 优点:不需要关心线程管理,数据同步的事情,可以把精力放在学要执行的操作上。基于gcd,是对gcd 的封装,比gcd更加面向对象缺点: nsoperation是个抽象类,使用它必须使用它的子类,可以实现它或者使用它定义好的两个子类nsinvocationoperation、nsblockoperation.

gcd 全称grand center dispatch (抽象层次:高)

优点:是 apple 开发的一个多核编程的解决方法,简单易用,效率高,速度快,基于c语言,更底层更高效,并且不是cocoa框架的一部分,自动管理线程生命周期(创建线程、调度任务、销毁线程)。

缺点: 使用gcd的场景如果很复杂,就有非常大的可能遇到死锁问题。

gcd抽象层次最高,使用也简单,因此,苹果也推荐使用gcd

gcd中的三种队列类型

gcd编程的核心就是dispatch队列,dispatch block的执行最终都会放进某个队列中去进行。

the main queue(主线程串行队列): 与主线程功能相同,提交至main queue的任务会在主线程中执行, main queue 可以通过dispatch_get_main_queue()来获取。 global queue(全局并发队列): 全局并发队列由整个进程共享,有高、中(默认)、低、后台四个优先级别。 global queue 可以通过调用dispatch_get_global_queue函数来获取(可以设置优先级) custom queue (自定义队列): 可以为串行,也可以为并发。 custom queue 可以通过dispatch_queue_create()来获取; group queue (队列组):将多线程进行分组,最大的好处是可获知所有线程的完成情况。 group queue 可以通过调用dispatch_group_create()来获取,通过dispatch_group_notify,可以直接监听组里所有线程完成情况。

gcd中相关函数的使用一般都是以dispatch开头

the main queue(主线程串行队列)

dispatch_sync 同步执行任务函数,不会开启新的线程,dispatch_async 异步执行任务函数,会开启新的线程

获取主线程串行队列
dispatch_queue_t mainqueue = dispatch_get_main_queue();
主线程串行队列同步执行任务,在主线程运行时,会产生死锁
dispatch_queue_t mainqueue = dispatch_get_main_queue();
dispatch_sync(mainqueue,^{
nslog("mainqueue");            
});
程序一直处于等待状态,block中的代码将执行不到主线程串行队列异步执行任务,在主线程运行,不会产生死锁。
dispatch_queue_t mainqueue = dispatch_get_main_queue();
dispatch_async(mainqueue,^{
nslog("mainqueue");            
});
程序正常运行,block中的代码正常运行从子线程,异步返回主线程更新ui<这种使用方式比较多>
dispatch_queue_t globalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
dispatch_async(globalqueue, ^{
    //子线程异步执行下载任务,防止主线程卡顿
    nsurl *url = [nsurl urlwithstring:@"https://www.baidu.com"];
    nserror *error;
    nsstring *htmldata = [nsstring stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];
    if (htmldata != nil) {
        dispatch_queue_t mainqueue = dispatch_get_main_queue();
         //异步返回主线程,根据获取的数据,更新ui
        dispatch_async(mainqueue, ^{
            nslog(@"根据更新ui界面");
        });
    } else {
        nslog(@"error when download:%@",error);
    }
});
主线程串行队列由系统默认生成的,所以无法调用dispatch_resume()和dispatch_suspend()来控制执行继续或中断。

global queue(全局并发队列)

 

耗时的操作,比如读取网络数据,io,读写等,我们会在另外一个线程中处理这些操作,然后通知主线程更新界面

 

获取全局并发队列
//程序默认的队列级别,一般不要修改,dispatch_queue_priority_default == 0
dispatch_queue_t globalqueue1 = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
//high
dispatch_queue_t globalqueue2 = dispatch_get_global_queue(dispatch_queue_priority_high, 0);
//low
dispatch_queue_t globalqueue3 = dispatch_get_global_queue(dispatch_queue_priority_low, 0);
//background
dispatch_queue_t globalqueue4 = dispatch_get_global_queue(dispatch_queue_priority_background, 0);
全局并发队列同步执行任务,在主线程执行会导致页面卡顿。
dispatch_queue_t globalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
nslog(@"current task");
dispatch_sync(globalqueue, ^{
    sleep(2.0);
    nslog(@"sleep 2.0s");
});
nslog(@"next task");
控制台输出如下:
2015-11-18 15:51:45.550 whisper[33152:345023] current task
2015-11-18 15:51:47.552 whisper[33152:345023] sleep 2.0s
2015-11-18 15:51:47.552 whisper[33152:345023] next task
2s钟之后,才会执行block代码段下面的代码。全局并发队列异步执行任务,在主线程运行,会开启新的子线程去执行任务,页面不会卡顿。
dispatch_queue_t globalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
nslog(@"current task");
dispatch_async(globalqueue, ^{
    sleep(2.0);
    nslog(@"sleep 2.0s");
});
nslog(@"next task");
控制台输出如下:
2015-11-18 15:50:14.999 whisper[33073:343781] current task
2015-11-18 15:50:15.000 whisper[33073:343781] next task
2015-11-18 15:50:17.004 whisper[33073:343841] sleep 2.0s
主线程不用等待2s钟,继续执行block代码段后面的代码。
多个全局并发队列,异步执行任务。
dispatch_queue_t globalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
nslog(@"current task");
dispatch_async(globalqueue, ^{
    nslog(@"最先加入全局并发队列");
});
dispatch_async(globalqueue, ^{
    nslog(@"次加入全局并发队列");
});
nslog(@"next task");
控制台输出如下:
2015-11-18 16:54:52.202 whisper[39827:403208] current task
2015-11-18 16:54:52.203 whisper[39827:403208] next task
2015-11-18 16:54:52.205 whisper[39827:403309] 最先加入全局并发队列
2015-11-18 16:54:52.205 whisper[39827:403291] 次加入全局并发队列
异步线程的执行顺序是不确定的。几乎同步开始执行
全局并发队列由系统默认生成的,所以无法调用dispatch_resume()和dispatch_suspend()来控制执行继续或中断。

custom queue (自定义队列)

自定义串行队列

获取自定义串行队列

dispatch_queue_t serialqueue = dispatch_queue_create("com.dullgrass.serialqueue", dispatch_queue_serial);
nslog(@"%s",dispatch_queue_get_label(concurrentqueue)) ;

控制台输出:

2015-11-19 11:05:34.469 whisper[1223:42960] com.dullgrass.serialqueue

dispatch_queue_create(const char *label, dispatch_queue_attr_t attr)函数中第一个参数是给这个queue起的标识,这个在调试的可以看到是哪个队列在执行,或者在crash日志中,也能做为提示。第二个是需要创建的队列类型,是串行的还是并发的

自定义串行队列同步执行任务

dispatch_queue_t serialqueue = dispatch_queue_create("com.dullgrass.serialqueue", dispatch_queue_serial);
nslog(@"current task");
dispatch_sync(serialqueue, ^{
nslog(@"最先加入自定义串行队列");
sleep(2);
});
dispatch_sync(serialqueue, ^{
nslog(@"次加入自定义串行队列");
});
nslog(@"next task");

控制台输出:

2015-11-18 17:09:40.025 whisper[40241:416296] current task
2015-11-18 17:09:40.027 whisper[40241:416296] 最先加入自定义串行队列
2015-11-18 17:09:43.027 whisper[40241:416296] 次加入自定义串行队列
2015-11-18 17:09:43.027 whisper[40241:416296] next task

当前线程等待串行队列中的子线程执行完成之后再执行,串行队列中先进来的子线程先执行任务,执行完成后,再执行队列中后面的任务。

自定义串行队列嵌套执行同步任务,产生死锁

dispatch_queue_t serialqueue = dispatch_queue_create("com.dullgrass.serialqueue", dispatch_queue_serial);
dispatch_sync(serialqueue, ^{   //该代码段后面的代码都不会执行,程序被锁定在这里
nslog(@"会执行的代码");
dispatch_sync(serialqueue, ^{
   nslog(@"代码不执行");
});
});

异步执行串行队列,嵌套同步执行串行队列,同步执行的串行队列中的任务将不会被执行,其他程序正常执行

dispatch_queue_t serialqueue = dispatch_queue_create("com.dullgrass.serialqueue", dispatch_queue_serial);
dispatch_async(serialqueue, ^{
nslog(@"会执行的代码");
dispatch_sync(serialqueue, ^{
   nslog(@"代码不执行");
});
});

 

注意不要嵌套使用同步执行的串行队列任务

 

自定义并发队列

获取自定义并发队列
dispatch_queue_t concurrentqueue =   dispatch_queue_create("com.dullgrass.concurrentqueue", dispatch_queue_concurrent);
自定义并发队列执行同步任务
dispatch_queue_t concurrentqueue = dispatch_queue_create("com.dullgrass.concurrentqueue", dispatch_queue_concurrent);
nslog(@"current task");
dispatch_sync(concurrentqueue, ^{
   nslog(@"先加入队列");
});
dispatch_sync(concurrentqueue, ^{
   nslog(@"次加入队列");
});
nslog(@"next task");
控制台输出如下:
2015-11-19 10:36:23.259 whisper[827:20596] current task
2015-11-19 10:36:23.261 whisper[827:20596] 先加入队列
2015-11-19 10:36:23.261 whisper[827:20596] 次加入队列
2015-11-19 10:36:23.261 whisper[827:20596] next task
自定义并发队列嵌套执行同步任务(不会产生死锁,程序正常运行)
dispatch_queue_t concurrentqueue = dispatch_queue_create("com.dullgrass.concurrentqueue", dispatch_queue_concurrent);
nslog(@"current task");
dispatch_sync(concurrentqueue, ^{
     nslog(@"先加入队列");
     dispatch_sync(concurrentqueue, ^{
         nslog(@"次加入队列");
     });
});
nslog(@"next task");
控制台输出如下:
2015-11-19 10:39:21.301 whisper[898:22273] current task
2015-11-19 10:39:21.303 whisper[898:22273] 先加入队列
2015-11-19 10:39:21.303 whisper[898:22273] 次加入队列
2015-11-19 10:39:21.303 whisper[898:22273] next task
自定义并发队列执行异步任务
dispatch_queue_t concurrentqueue = dispatch_queue_create("com.dullgrass.concurrentqueue", dispatch_queue_concurrent);
nslog(@"current task");
dispatch_async(concurrentqueue, ^{
   nslog(@"先加入队列");
});
dispatch_async(concurrentqueue, ^{
   nslog(@"次加入队列");
});
nslog(@"next task");
控制台输出如下:
2015-11-19 10:45:22.290 whisper[1050:26445] current task
2015-11-19 10:45:22.290 whisper[1050:26445] next task
2015-11-19 10:45:22.290 whisper[1050:26505] 次加入队列
2015-11-19 10:45:22.290 whisper[1050:26500] 先加入队列

异步执行任务,开启新的子线程,不影响当前线程任务的执行,并发队列中的任务,几乎是同步执行的,输出顺序不确定

group queue (队列组)

当遇到需要执行多个线程并发执行,然后等多个线程都结束之后,再汇总执行结果时可以用group queue

使用场景: 同时下载多个图片,所有图片下载完成之后去更新ui(需要回到主线程)或者去处理其他任务(可以是其他线程队列)。原理:使用函数dispatch_group_create创建dispatch group,然后使用函数dispatch_group_async来将要执行的block任务提交到一个dispatch queue。同时将他们添加到一个组,等要执行的block任务全部执行完成之后,使用dispatch_group_notify函数接收完成时的消息。

使用示例:

dispatch_queue_t concurrentglobalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
dispatch_queue_t mainqueue = dispatch_get_main_queue();
dispatch_group_t groupqueue = dispatch_group_create();
nslog(@"current task");
dispatch_group_async(groupqueue, concurrentglobalqueue, ^{
   nslog(@"并行任务1");
});
dispatch_group_async(groupqueue, concurrentglobalqueue, ^{
   nslog(@"并行任务2");
});
dispatch_group_notify(groupqueue, mainqueue, ^{
   nslog(@"groupqueue中的任务 都执行完成,回到主线程更新ui");
});
nslog(@"next task");

控制台输出:

2015-11-19 13:47:55.117 whisper[1645:97116] current task
2015-11-19 13:47:55.117 whisper[1645:97116] next task
2015-11-19 13:47:55.119 whisper[1645:97178] 并行任务1
2015-11-19 13:47:55.119 whisper[1645:97227] 并行任务2
2015-11-19 13:47:55.171 whisper[1645:97116] groupqueue中的任务 都执行完成,回到主线程更新ui

在当前线程阻塞的同步等待dispatch_group_wait

dispatch_group_t groupqueue = dispatch_group_create();
dispatch_time_t delaytime = dispatch_time(dispatch_time_now, 10 * nsec_per_sec);
dispatch_queue_t concurrentglobalqueue = dispatch_get_global_queue(dispatch_queue_priority_default, 0);
nslog(@"current task");
dispatch_group_async(groupqueue, concurrentglobalqueue, ^{

   long isexecuteover = dispatch_group_wait(groupqueue, delaytime);
   if (isexecuteover) {
       nslog(@"wait over");
   } else {
       nslog(@"not over");
   }
   nslog(@"并行任务1");
});
dispatch_group_async(groupqueue, concurrentglobalqueue, ^{
   nslog(@"并行任务2");
});

控制台输出如下:

2015-11-19 14:37:29.514 whisper[2426:126683] current task
2015-11-19 14:37:29.518 whisper[2426:126791] 并行任务2
2015-11-19 14:37:39.515 whisper[2426:126733] wait over
2015-11-19 14:37:39.516 whisper[2426:126733] 并行任务1

dispatch_time(dispatch_time_t when, int64_t delta);
参数注释:
第一个参数一般是dispatch_time_now,表示从现在开始
第二个参数是延时的具体时间
延时1秒可以写成如下几种:
nsec_per_sec----每秒有多少纳秒
dispatch_time(dispatch_time_now, 1*nsec_per_sec);
usec_per_sec----每秒有多少毫秒(注意是指在纳秒的基础上)
dispatch_time(dispatch_time_now, 1000*usec_per_sec); //sec---毫秒
nsec_per_usec----每毫秒有多少纳秒。
dispatch_time(dispatch_time_now, usec_per_sec*nsec_per_usec);sec---纳秒

gcd中一些系统提供的常用dispatch方法

dispatch_after延时添加到队列

使用示例:
dispatch_time_t delaytime3 = dispatch_time(dispatch_time_now, 3*nsec_per_sec);
dispatch_time_t delaytime2 = dispatch_time(dispatch_time_now, 2*nsec_per_sec);
dispatch_queue_t mainqueue = dispatch_get_main_queue();
nslog(@"current task");
dispatch_after(delaytime3, mainqueue, ^{
  nslog(@"3秒之后添加到队列");
});
dispatch_after(delaytime2, mainqueue, ^{
   nslog(@"2秒之后添加到队列");
});
nslog(@"next task");
控制台输出如下:
2015-11-19 15:50:19.369 whisper[2725:172593] current task
2015-11-19 15:50:19.370 whisper[2725:172593] next task
2015-11-19 15:50:21.369 whisper[2725:172593] 2秒之后添加到队列
2015-11-19 15:50:22.654 whisper[2725:172593] 3秒之后添加到队列

dispatch_after只是延时提交block,并不是延时后立即执行,并不能做到精确控制,需要精确控制的朋友慎用哦

dispatch_apply在给定的队列上多次执行某一任务,在主线程直接调用会阻塞主线程去执行block中的任务。

dispatch_apply函数的功能:把一项任务提交到队列中多次执行,队列可以是串行也可以是并行,dispatch_apply不会立刻返回,在执行完block中的任务后才会返回,是同步执行的函数。dispatch_apply正确使用方法:为了不阻塞主线程,一般把dispatch_apply放在异步队列中调用,然后执行完成后通知主线程使用示例:
dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
nslog(@"current task");
dispatch_async(globalqueue, ^{
dispatch_queue_t applyqueue = dispatch_get_global_queue(0, 0);
//第一个参数,3--block执行的次数
//第二个参数,applyqueue--block任务提交到的队列
//第三个参数,block--需要重复执行的任务
dispatch_apply(3, applyqueue, ^(size_t index) {
      nslog(@"current index %@",@(index));
      sleep(1);
});
nslog(@"dispatch_apply 执行完成");
dispatch_queue_t mainqueue = dispatch_get_main_queue();
dispatch_async(mainqueue, ^{
      nslog(@"回到主线程更新ui");
});
});
nslog(@"next task");
控制台输出如下:
2015-11-19 16:24:45.015 whisper[4034:202269] current task
2015-11-19 16:24:45.016 whisper[4034:202269] next task
2015-11-19 16:24:45.016 whisper[4034:202347] current index 0
2015-11-19 16:24:45.016 whisper[4034:202344] current index 1
2015-11-19 16:24:45.016 whisper[4034:202345] current index 2
2015-11-19 16:24:46.021 whisper[4034:202347] dispatch_apply 执行完成
2015-11-19 16:24:46.021 whisper[4034:202269] 回到主线程更新ui
嵌套使用dispatch_apply会导致死锁。

dispatch_once保证在app运行期间,block中的代码只执行一次

经典使用场景---单例单例对象sharemanager的定义:
 sharemanager的.h文件
#import 
@interface sharemanager : nsobject
@property (nonatomic, copy) nsstring *someproperty;
+ (sharemanager *)sharemanager;
+ (sharemanager *)sharedmanager;
@end

sharemanager的.m文件
#import "sharemanager.h"
@implementation sharemanager
static sharemanager *sharedmanager = nil;
//gcd实现单例功能
+ (sharemanager *)sharemanager
{
 static dispatch_once_t oncetoken;
 dispatch_once(&oncetoken, ^{
     sharedmanager = [[self alloc] init];
 });
 return sharedmanager;
}
//在arc下,非gcd,实现单例功能
+ (sharemanager *)sharedmanager
{
 @synchronized(self) {
     if (!sharedmanager) {
         sharedmanager = [[self alloc] init];
     }
 }
 return sharedmanager;
}
- (instancetype)init{
 self = [super init];
 if (self) {
      _someproperty =@"default property value";
 }
 return self;
}
@end

sharemanager的使用
#import "sharemanager.h"
在需要使用的函数中,直接调用下面的方法
sharemanager *share = [sharemanager sharedmanager];
nslog(@"share is %@",share.someproperty);
dispatch_barrier_async 栅栏的作用 功能:是在并行队列中,等待在dispatch_barrier_async之前加入的队列全部执行完成之后(这些任务是并发执行的)再执行dispatch_barrier_async中的任务,dispatch_barrier_async中的任务执行完成之后,再去执行在dispatch_barrier_async之后加入到队列中的任务(这些任务是并发执行的)。使用示例:
dispatch_queue_t concurrentqueue = dispatch_queue_create("com.dullgrass.concurrentqueue", dispatch_queue_concurrent);
dispatch_async(concurrentqueue, ^{
  nslog(@"dispatch 1");
});
dispatch_async(concurrentqueue, ^{
   nslog(@"dispatch 2");
});
dispatch_barrier_async(concurrentqueue, ^{
   nslog(@"dispatch barrier");
});
dispatch_async(concurrentqueue, ^{
   nslog(@"dispatch 3");
});
dispatch_async(concurrentqueue, ^{
   nslog(@"dispatch 4");
});
控制台输出如下:
2015-11-19 18:12:34.125 whisper[22633:297257] dispatch 1
2015-11-19 18:12:34.125 whisper[22633:297258] dispatch 2
2015-11-19 18:12:34.126 whisper[22633:297258] dispatch barrier
2015-11-19 18:12:34.127 whisper[22633:297258] dispatch 3
2015-11-19 18:12:34.127 whisper[22633:297257] dispatch 4

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网