image.png

前言

Grand Central Dispatch(GCD) 是 Apple 开发的一个多核编程的较新的解决办法。它主要用于优化应用程序以支撑多核处理器以及其他对称多处理系统。它是一个在线程池形式的基础上履行的并发使命。在 Mac OS X 10.6 雪豹中首次推出,也可在iOS 4 及以上版别运用。

GCD的特色

GCD会主动运用更多的CPU内核
GCD主动办理线程的生命周期(创立线程,调度使命,销毁线程等)。
程序员只需求告知 GCD 想要如何履行什么使命,不需求编写任何线程办理代码。

大局并发行列的优先级

# iOS 8 服务质量
QOS_CLASS_USER_INTERACTIVE 用户交互(希望线程快速被履行,不要用好使的操作)
QOS_CLASS_USER_INITIATED 用户需求的(同样不要运用耗时操作)
QOS_CLASS_DEFAULT 默许的(给系统来重置行列的)
QOS_CLASS_UTILITY 运用工具(用来做耗时操作)
QOS_CLASS_BACKGROUND 后台
QOS_CLASS_UNSPECIFIED 没有指定优先级
# iOS 7 调度的优先级
DISPATCH_QUEUE_PRIORITY_HIGH 2 高优先级
DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默许优先级
DISPATCH_QUEUE_PRIORITY_LOW (-2) 低优先级
DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 后台优先级
提示:尤其不要挑选BACKGROUND 优先级,服务质量,线程履行会慢到令人发指!!!

行列组 dispatch_group,关键词有

dispatch_group_async
dispatch_group_notify
dispatch_group_enter
dispatch_group_leave
dispatch_group_wait

延时履行 常用办法

//办法1
[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
//办法2
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
});

GCD 线程间通讯

dispatch_async(dispatch_get_global_queue(0, 0), ^{
      //做其他事。。。。。
      //回主线程
      dispatch_async(dispatch_get_main_queue(), ^{
      })
})

运用dispatch_once 函数能确保某段代码在程序运转过程中只被履行一次

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只履行一次的代码(这儿边默许是线程安全的,内部加了锁,应该是互斥锁)
});
运用dispatch_apply函数能进行快速迭代遍历
dispatch_apply(10, dispatch_get_global_queue(0, 0), ^(size_t index) {
//履行10次代码,index次序不确定
})
一:串行同步
/** 串行同步: 履行完一个使命,再履行下一个使命。不敞开新线程。 */
- (void)syncSerial {
     NSLog(@"\n\n**************串行同步***************\n\n");
    // 串行行列  DISPATCH_QUEUE_SERIAL 串行
    dispatch_queue_t queue = dispatch_queue_create("XYText", DISPATCH_QUEUE_SERIAL);
    // 同步履行
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"串行同步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"串行同步2   %@",[NSThread currentThread]);
        }
    });
    /*
     输入成果为次序履行,都在主线程:
     串行同步1   {number = 1, name = main}
     串行同步1   {number = 1, name = main}
     串行同步1   {number = 1, name = main}
     串行同步2   {number = 1, name = main}
     串行同步2   {number = 1, name = main}
     串行同步2   {number = 1, name = main}
     */
}
二:串行异步
/** 串行异步  敞开新线程,但由于使命是串行的,所以还是按次序履行使命 */
- (void)asyncSerial {
     NSLog(@"\n\n**************串行异步***************\n\n");
     // 串行行列  DISPATCH_QUEUE_SERIAL 串行
    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
    // 异步履行
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"串行异步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"串行异步2   %@",[NSThread currentThread]);
        }
    });
 /*
  输入成果为次序履行,有不同线程:
  串行异步1   {number = 3, name = (null)}
  串行异步1   {number = 3, name = (null)}
  串行异步1   {number = 3, name = (null)}
  串行异步2   {number = 3, name = (null)}
  串行异步2   {number = 3, name = (null)}
  串行异步2   {number = 3, name = (null)}
  */
}
三:并发同步
/** 并发同步 由于是同步的,所以履行完一个使命,再履行下一个使命。不会敞开新线程 */
- (void)syncConcurrent {
    NSLog(@"\n\n**************并发同步***************\n\n");
     // 并发行列  DISPATCH_QUEUE_CONCURRENT 并发
    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
    // 同步履行
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"并发同步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"并发同步2   %@",[NSThread currentThread]);
        }
    });
    /*
     输入成果为次序履行,都在主线程:
     并发同步1   {number = 1, name = main}
     并发同步1   {number = 1, name = main}
     并发同步1   {number = 1, name = main}
     并发同步2   {number = 1, name = main}
     并发同步2   {number = 1, name = main}
     并发同步2   {number = 1, name = main}
     */
}
四:并发异步
/** 并发异步 使命替换履行,敞开多线程。 */
-(void)asyncConcurrent{
    NSLog(@"\n\n**************并发异步***************\n\n");
    // 并发行列 DISPATCH_QUEUE_CONCURRENT 并发
    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
    // 异步履行
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"并发异步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"并发异步2   %@",[NSThread currentThread]);
        }
    });
    /*
     输入成果为无序履行,有多条线程:
     并发异步1   {number = 3, name = (null)}
     并发异步2   {number = 4, name = (null)}
     并发异步1   {number = 3, name = (null)}
     并发异步2   {number = 4, name = (null)}
     并发异步1   {number = 3, name = (null)}
     并发异步2   {number = 4, name = (null)}
     */
}
五: 主行列同步
/** 主行列同步 假如在主线程中运用这种方法,则会产生死锁,程序崩溃。 */
-(void)syncMain {
    NSLog(@"\n\n**************主行列同步,放到主线程会死锁, 程序崩溃***************\n\n");
    // 主行列
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"主行列同步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"主行列同步2   %@",[NSThread currentThread]);
        }
    });
}
六:主行列异步
/** 主行列异步 在主线程中使命按次序履行 */
- (void)asyncMain {
    NSLog(@"\n\n**************主行列异步***************\n\n");
    // 主行列
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"主行列异步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"主行列异步2   %@",[NSThread currentThread]);
        }
    });
    /*
     输入成果为在主线程中按次序履行:
     主行列异步1   {number = 1, name = main}
     主行列异步1   {number = 1, name = main}
     主行列异步1   {number = 1, name = main}
     主行列异步2   {number = 1, name = main}
     主行列异步2   {number = 1, name = main}
     主行列异步2   {number = 1, name = main}
     */
}
七:GCD线程之间的通讯
/**
 开发中需求在主线程上进行UI的相关操作,一般会把一些耗时的操作放在其他线程,比如说图片文件下载等耗时操作。
 当完结了耗时操作之后,需求回到主线程进行UI的处理,这儿就用到了线程之间的通讯。
 */
- (IBAction)communicationBetweenThread:(id)sender {
    // 异步
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 耗时操作放在这儿,例如下载图片。(运用线程休眠两秒来模仿耗时操作)
        [NSThread sleepForTimeInterval:2];
        NSString *picURLStr = @"http://www.bangmangxuan.net/uploads/allimg/160320/74-160320130500.jpg";
        NSURL *picURL = [NSURL URLWithString:picURLStr];
        NSData *picData = [NSData dataWithContentsOfURL:picURL];
        UIImage *image = [UIImage imageWithData:picData];
        // 回到主线程处理UI
        dispatch_async(dispatch_get_main_queue(), ^{
            // 在主线程上增加图片
           // self.imageView.image = image;
        });
    });
}
八:GCD栅门

栅门 : 将两组异步履行的操作组给分割起来。等第一组履行完再履行第二组dispatch_barrier_async

/**
当使命需求异步进行,可是这些使命需求分红两组来履行,第一组完结之后才能进行第二组的操作。这时分就用了到GCD的栅门办法dispatch_barrier_async。
 */
- (IBAction)barrierGCD:(id)sender {
    // 并发行列
    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
    // 异步履行
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"栅门:并发异步1   %@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"栅门:并发异步2   %@",[NSThread currentThread]);
        }
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"------------barrier------------%@", [NSThread currentThread]);
        NSLog(@"******* 并发异步履行,可是34必定在12后边 *********");
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"栅门:并发异步3   %@",[NSThread currentThread]);
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            NSLog(@"栅门:并发异步4   %@",[NSThread currentThread]);
        }
    });
    /*
     上面代码的打印成果如下,敞开了多条线程,一切使命都是并发异步进行。可是第一组完结之后,才会进行第二组的操作。
     栅门:并发异步1   {number = 3, name = (null)}
     栅门:并发异步2   {number = 6, name = (null)}
     栅门:并发异步1   {number = 3, name = (null)}
     栅门:并发异步2   {number = 6, name = (null)}
     栅门:并发异步1   {number = 3, name = (null)}
     栅门:并发异步2   {number = 6, name = (null)}
     ------------barrier------------{number = 6, name = (null)}
     ******* 并发异步履行,可是34必定在12后边 *********
     栅门:并发异步4   {number = 3, name = (null)}
     栅门:并发异步3   {number = 6, name = (null)}
     栅门:并发异步4   {number = 3, name = (null)}
     栅门:并发异步3   {number = 6, name = (null)}
     栅门:并发异步4   {number = 3, name = (null)}
     栅门:并发异步3   {number = 6, name = (null)}
     */
}
九: GCD延时履行
/**当需求等候一会再履行一段代码时,就能够用到这个办法了:dispatch_after。*/
-(void)yanshhi{
   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 5秒后异步履行
        NSLog(@"我现已等候了5秒!");
    });
//    GCD实现代码只履行一次
//    运用dispatch_once能确保某段代码在程序运转过程中只被履行1次。能够用来设计单例。
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"程序运转过程中我只履行了一次!");
    });
}
十: GCD行列组

关键词有

dispatch_group_async
dispatch_group_notify
dispatch_group_enter
dispatch_group_leave
dispatch_group_wait
/**
 异步履行几个耗时操作,当这几个操作都完结之后再回到主线程进行操作,就能够用到行列组了。
 行列组有下面几个特色:
 一切的使命会并发的履行(不按序)。
 一切的异步函数都增加到行列中,然后再纳入行列组的监听规模。
 运用dispatch_group_notify函数,来监听上面的使命是否完结,假如完结, 就会调用这个办法。
 */
/**
 * 行列组 dispatch_group_notify
 */
- (void)groupNotify {
    NSLog(@"currentThread---%@",[NSThread currentThread]);  // 打印当时线程
    NSLog(@"group---begin");
    dispatch_group_t group =  dispatch_group_create();
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加使命 1
        [NSThread sleepForTimeInterval:2];              // 模仿耗时操作
        NSLog(@"1---%@",[NSThread currentThread]);      // 打印当时线程
    });
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加使命 2
        [NSThread sleepForTimeInterval:2];              // 模仿耗时操作
        NSLog(@"2---%@",[NSThread currentThread]);      // 打印当时线程
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        // 等前面的异步使命 1、使命 2 都履行完毕后,回到主线程履行下边使命
        [NSThread sleepForTimeInterval:2];              // 模仿耗时操作
        NSLog(@"3---%@",[NSThread currentThread]);      // 打印当时线程
        NSLog(@"group---end");
    });
}
十一: GCD线程之间的依靠关系

a使命开端履行的条件是b使命履行完结,
c使命开端履行需求等a、b两个异步使命完结,
a依靠于bc又依靠a,b,这种需求咱们能够运用的GCD来处理。

//创立分组
 dispatch_group_t group =dispatch_group_create();
 //创立行列
 dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_CONCURRENT);
  //往分组中增加使命
 dispatch_group_async(group, queue, ^{
  [NSThreadsleepForTimeInterval:2];//模仿耗时操作
   NSLog(@"11111 %@", [NSThreadcurrentThread]);
 });
 //往分组中增加使命
 dispatch_group_async(group, queue, ^{
  [NSThreadsleepForTimeInterval:1];//模仿耗时操作
   NSLog(@"2222 %@", [NSThreadcurrentThread]);
 });
 //分组中使命完结今后告诉该block履行
 dispatch_group_notify(group, queue, ^{
  NSLog(@"完结 %@", [NSThreadcurrentThread]);
  dispatch_async(dispatch_get_main_queue(), ^{
  NSLog(@"告诉主线程改写UI %@",[NSThreadcurrentThread]);
 });
 });
// 履行成果:
2017-10-24 11:33:40.426 iOSTest[34497:828682] 2222 0x600000260080>{number = 3, name = (null)} 
2017-10-24 11:33:41.426 iOSTest[34497:828660] 11111 0x608000261d80>{number = 4, name = (null)} 
2017-10-2411:33:41.427 iOSTest[34497:828660] 完结 0x608000261d80>{number = 4, name = (null)} 
2017-10-2411:33:41.427 iOSTest[34497:828365] 告诉主线程改写UI 0x60800007b980>{number = 1, name = main}
这样咱们运用group能够实现几个使命之间的依靠关系。

有时咱们的使命一层一层的嵌套了多个Block,这个时分,就应该运用如下代码方法:

    //创立分组
    dispatch_group_t group =dispatch_group_create();
    //创立行列
    dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_CONCURRENT);
    //往分组中增加使命
    dispatch_group_async(group, queue, ^{
         void (^task)(void) = ^{
            [NSThreadsleepForTimeInterval:2];//模仿耗时操作
             NSLog(@"11111 %@", [NSThreadcurrentThread]);
      };
        dispatch_async(dispatch_get_global_queue(0,0), task);
            NSLog(@"11111---- %@", [NSThreadcurrentThread]);
    });
    //往分组中增加使命
    dispatch_group_async(group, queue, ^{
        void (^task)(void) = ^ {
            [NSThreadsleepForTimeInterval:1];//模仿耗时操作
             NSLog(@"2222 %@", [NSThreadcurrentThread]);
        };
        dispatch_async(dispatch_get_global_queue(0,0), task);
            NSLog(@"2222------- %@", [NSThreadcurrentThread]);
      });
    //分组中使命完结今后告诉该block履行
    dispatch_group_notify(group, queue, ^{
           NSLog(@"完结 %@", [NSThreadcurrentThread]);
         dispatch_async(dispatch_get_main_queue(), ^{
           NSLog(@"告诉主线程改写UI %@", [NSThreadcurrentThread]);
       });
    });
履行成果:
2017-10-24 11:44:06.447 iOSTest[34981:881063] 2222------- {number = 4, name = (null)}
2017-10-2411:44:06.447 iOSTest[34981:881046] 11111---- {number = 3, name = (null)}
2017-10-24 11:44:06.448iOSTest[34981:881046] 完结 0x600000071f40>{number = 3, name = (null)} 
2017-10-24 11:44:06.450iOSTest[34981:880987] 告诉主线程改写UI 0x60000006c340>{number = 1, name = main}
 2017-10-2411:44:07.450 iOSTest[34981:881064] 2222 0x6000000708c0>{number = 5, name = (null)} 
2017-10-2411:44:08.452 iOSTest[34981:881049] 11111 0x61000006d5c0>{number = 6, name = (null)}

根据履行成果能够看出,当主线程履行的时分,可是其他两个使命中并没有真实的完结,由于另外两个使命中嵌套了子使命,那问题来了,其他两个使命还没有完结就履行主线程,可是咱们需求的是其他两个使命完结才需求履行主线程,别急,group给咱们提供了dispatch_group_enter()与dispatch_group_leave()办法来组合运用,值得注意的是,这两个办法必定需求成对运用,要不然有时间又呈现一些莫名其妙的bug问题。
代码如下:

 //创立分组
 dispatch_group_t group =dispatch_group_create();
 //创立行列
 dispatch_queue_t queue =dispatch_queue_create("queue",DISPATCH_QUEUE_CONCURRENT);
 //往分组中增加使命
 dispatch_group_enter(group);
    dispatch_async(queue, ^{
     void (^task)(void) = ^{
        [NSThreadsleepForTimeInterval:2];//模仿耗时操作
         NSLog(@"11111 %@", [NSThreadcurrentThread]);
         dispatch_group_leave(group);
    };
   dispatch_async(dispatch_get_global_queue(0,0), task);
         NSLog(@"11111---- %@",[NSThreadcurrentThread]);
 });
    //往分组中增加使命
  dispatch_group_enter(group);
  dispatch_async(queue, ^{
    void (^task)(void) = ^ {
         [NSThreadsleepForTimeInterval:1];//模仿耗时操作
         NSLog(@"2222 %@", [NSThreadcurrentThread]);
         dispatch_group_leave(group);
  };
     dispatch_async(dispatch_get_global_queue(0,0), task);
     NSLog(@"2222------- %@", [NSThreadcurrentThread]);
  });
    //分组中使命完结今后告诉该block履行
   dispatch_group_notify(group, queue, ^{
       NSLog(@"完结 %@", [NSThreadcurrentThread]);
   dispatch_async(dispatch_get_main_queue(), ^{
       NSLog(@"告诉主线程改写UI %@", [NSThreadcurrentThread]);
   });
 });
履行成果如下:
2017-10-24 11:48:05.641 iOSTest[35128:902591] 11111---- {number = 3, name = (null)}
2017-10-2411:48:05.641 iOSTest[35128:902608] 2222------- {number = 4, name = (null)}
2017-10-24 11:48:06.644iOSTest[35128:902609] 2222 0x60000006cb00>{number = 5, name = (null)} 
2017-10-24 11:48:07.644iOSTest[35128:902593] 11111 0x6080000721c0>{number = 6, name = (null)} 
2017-10-24 11:48:07.644iOSTest[35128:902593] 完结 0x6080000721c0>{number = 6, name = (null)} 
2017-10-24 11:48:07.645iOSTest[35128:902524] 告诉主线程改写UI 0x61000006e280>{number = 1, name = main}

这样咱们想要的得到的成果就实现了。

A,B,C三个使命并发履行,可是C要等A,B履行完结之后再履行。

信号量:

// 创立一个信号,value:信号量
dispatch_semaphore_create(<#long value#>)
// 使某个信号的信号量+1
dispatch_semaphore_signal(<#dispatch_semaphore_t dsema#>)
// 某个信号进行等候或等候降低信号量 timeout:等候时间,永远等候为 DISPATCH_TIME_FOREVER
dispatch_semaphore_wait(<#dispatch_semaphore_t dsema#>, <#dispatch_time_t timeout#>)
正常的运用次序是先降低然后再提高,这两个函数一般成对运用。

两种办法

-(void)dispatch_group_function1
{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, dispatch_queue_create("com.dispatch.test", DISPATCH_QUEUE_CONCURRENT), ^{
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
        NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // 恳求完结,能够告诉界面改写界面等操作
            NSLog(@"第一步网络恳求完结");
            // 使信号的信号量+1,这儿的信号量原本为0,+1信号量为1(绿灯)
            dispatch_semaphore_signal(semaphore);
        }];
        [task resume];
        // 以下还要进行一些其他的耗时操作
        NSLog(@"耗时操作持续进行");
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });
    dispatch_group_async(group, dispatch_queue_create("com.dispatch.test", DISPATCH_QUEUE_CONCURRENT), ^{
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.github.com"]];
        NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // 恳求完结,能够告诉界面改写界面等操作
            NSLog(@"第二步网络恳求完结");
            // 使信号的信号量+1,这儿的信号量原本为0,+1信号量为1(绿灯)
            dispatch_semaphore_signal(semaphore);
        }];
        [task resume];
        // 以下还要进行一些其他的耗时操作
        NSLog(@"耗时操作持续进行");
        dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"改写界面等在主线程的操作");
    });
}
2019-02-24 18:19:16.251067+0800 Semaphore[33094:12267734] 耗时操作持续进行
2019-02-24 18:19:16.251071+0800 Semaphore[33094:12267735] 耗时操作持续进行
2019-02-24 18:19:16.549563+0800 Semaphore[33094:12267748] 第一步网络恳求完结
2019-02-24 18:19:18.091922+0800 Semaphore[33094:12267737] 第二步网络恳求完结
2019-02-24 18:19:18.092222+0800 Semaphore[33094:12267662] 改写界面等在主线程的操作

设定的信号值为2,先履行两个线程,等履行完一个,才会持续履行下一个,确保同一时间履行的线程数不超过2。

dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //使命1
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"run task 1");
        sleep(1);
        NSLog(@"complete task 1");
        dispatch_semaphore_signal(semaphore);
    });
    //使命2
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"run task 2");
        sleep(1);
        NSLog(@"complete task 2");
        dispatch_semaphore_signal(semaphore);
    });
    //使命3
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"run task 3");
        sleep(1);
        NSLog(@"complete task 3");
        dispatch_semaphore_signal(semaphore);
    });
2019-02-24 18:31:02.447769+0800 Semaphore[33286:12284312] run task 1
2019-02-24 18:31:02.447767+0800 Semaphore[33286:12284310] run task 2
2019-02-24 18:31:03.450756+0800 Semaphore[33286:12284312] complete task 1
2019-02-24 18:31:03.450756+0800 Semaphore[33286:12284310] complete task 2
2019-02-24 18:31:03.450997+0800 Semaphore[33286:12284311] run task 3
2019-02-24 18:31:04.454259+0800 Semaphore[33286:12284311] complete task 3