多线程

GCD多线程并发执行多任务并同步返回

2019-03-05  本文已影响0人  methodname

前面有写过一篇 .NET 使用WaitHandle开启并发多线程查询并同步返回

在.NET中有WaitHandle可以进行多任务多线程的操作,作为成熟的语言OC中是否也有同样的存在,于是我将目光转向了苹果杰作GCD.

终于,我在官方的文档中找到了 dispatch_block_wait

dispatch_group_wait

同步等待先前提交的块对象完成; 如果在指定的超时时间结束之前块没有完成,则返回。

参数

可以看到以上需要两个参数:一是执行等待任务的调度数组,二是等待超时的时间;

代码

//任务1
    dispatch_block_t t_block1 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
       for (int i =0; i<10; i ++) {
           printf("dispatch_block_create1\n");
       }
    });

    //任务2
    dispatch_block_t t_block2 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
        for (int i =0; i<5; i ++) {
            printf("dispatch_block_create2\n");
        }
    });

    //任务3
    dispatch_block_t t_block3 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, ^{
        for (int i =0; i<30; i ++) {
            printf("dispatch_block_create3\n");
        }
    });

    //创建任务组
    dispatch_group_t block_group = dispatch_group_create();

    //创建队列
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);

    //向任务组中添加异步任务【将块提交到调度队列,并将块与指定的调度组相关联。】
    dispatch_group_async(block_group, aQueue, t_block1);
    dispatch_group_async(block_group, aQueue, t_block2);
    dispatch_group_async(block_group, aQueue, t_block3);

    printf("BEGIN\n");
    //执行等待任务组,所有任务完成后返回(任务组,等待分派任务延迟时间)此处开启多线程异步执行多任务
    dispatch_group_wait(block_group, dispatch_time(0, 10000000));
    printf("END\n");

输出结果

BEGIN
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create2
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create1
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
dispatch_block_create3
END

dispatch_group_wait 在所有的任务执行完成后再返回,并且是多线程异步任务,多任务操作的时间即可以大大缩小,以上都是基于block的方式,苹果也提供了另外一种添加调度队列任务

void dispatch_group_async_f(dispatch_group_t group, dispatch_queue_t queue, void *context, dispatch_function_t work);

参数
function版本
 dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);

    dispatch_group_t function_group = dispatch_group_create();

    // 为结构体分配内存
    gcd_f_data *context = (gcd_f_data *) malloc(sizeof(gcd_f_data));
    // 初始化结构体
    if (context != NULL){
        context->index = 0;
        context->title = "GCD1";
        // GCD执行异步方法:指定主队列(mainQueue),传递结构体数据(context)来执行displayAlertView方法。
        dispatch_group_async_f(function_group,aQueue,(void *)context, fun);
    }

    // 为结构体分配内存
    gcd_f_data *context2 = (gcd_f_data *) malloc(sizeof(gcd_f_data));
    // 初始化结构体
    if (context2 != NULL){
        context2->index = 1;
        context2->title = "GCD2";
        // GCD执行异步方法:指定主队列(mainQueue),传递结构体数据(context)来执行displayAlertView方法。
        dispatch_group_async_f(function_group,aQueue,(void *)context2, fun);
    }

    // 为结构体分配内存
    gcd_f_data *context3 = (gcd_f_data *) malloc(sizeof(gcd_f_data));
    // 初始化结构体
    if (context3 != NULL){
        context3->index = 2;
        context3->title = "GCD3";
        // GCD执行异步方法:指定主队列(mainQueue),传递结构体数据(context)来执行displayAlertView方法。
        dispatch_group_async_f(function_group,aQueue,(void *)context3, fun);
    }
    printf("BEGIN\n");
    dispatch_group_wait(function_group, dispatch_time(0, 10000000));
    printf("END\n");

//执行任务的function
void fun(void * context)
{
    gcd_f_data *data = (gcd_f_data *)context;
    int indnx = 0;
    if (data->index == 0) {
        indnx = 10;
    }else if (data->index == 1) {
        indnx = 5;
    }else if (data->index == 2) {
        indnx = 25;
    }
    for (int i =0; i<indnx; i++) {
        printf("%s\n",data->title);
    }
}

//自定义数据结构体
typedef struct{
    int index;
    char *title;
} gcd_f_data;

定义一个宏

/**
 等待并发任务同步返回

 @param array 任务数组(blocks)
 @param timeout 延迟时间(纳秒数)
 @return void
 */
#define waitQueueGroup(array,timeout) dispatch_group_t block_group = dispatch_group_create();dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_BLOCK_DETACHED, 0);for (id item in array) {dispatch_block_t t_block1 = dispatch_block_create(DISPATCH_BLOCK_DETACHED, item);dispatch_group_async(block_group, aQueue, t_block1);}dispatch_group_wait(block_group, dispatch_time(0, timeout));

使用示例

    //创建任务数组
    NSArray * array = @[^(void){
        for (int i = 0; i< 10; i ++) {
            printf("hello world 1\n");
        }
    },^(void){
        for (int i = 0; i< 20; i ++) {
            printf("hello world 2\n");
        }
    },^(void){
        for (int i = 0; i< 5; i ++) {
            printf("hello world 3\n");
        }
    }];
    //调用
    waitQueueGroup(array, 10000000);

END

上一篇下一篇

猜你喜欢

热点阅读