iOS 线程列队依赖

iOS的日常业务开发中,有的时候会出现诸如任务C和D要等待任务A和B结束,需要A和B的数据支持,此类的线程列队依赖场景。
以下列举三种常用的方法供大家学习参考。

1.信号量dispatch_semaphore

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        NSLog(@"run task 1");
        sleep(1);
        NSLog(@"complete task 1");
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"run task 2");
        sleep(1);
        NSLog(@"complete task 2");
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
    dispatch_async(queue, ^{
        NSLog(@"run task 3");
        sleep(1);
        NSLog(@"complete task 3");
    });
    
    dispatch_async(queue, ^{
        NSLog(@"run task 4");
        sleep(1);
        NSLog(@"complete task 4");
    });
2020-09-09 14:55:00.023807+0800 Test[8144:782166] run task 1
2020-09-09 14:55:00.023807+0800 Test[8144:782169] run task 2
2020-09-09 14:55:01.024233+0800 Test[8144:782169] complete task 2
2020-09-09 14:55:01.024233+0800 Test[8144:782166] complete task 1
2020-09-09 14:55:01.024651+0800 Test[8144:782169] run task 3
2020-09-09 14:55:01.024673+0800 Test[8144:782166] run task 4
2020-09-09 14:55:02.029937+0800 Test[8144:782169] complete task 3
2020-09-09 14:55:02.029957+0800 Test[8144:782166] complete task 4

2.栅栏函数dispatch_barrier

dispatch_queue_t concurrentQueue = dispatch_queue_create("EpisodeConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(concurrentQueue, ^{
        NSLog(@"run task 1");
        sleep(1);
        NSLog(@"complete task 1");
    });
    
    dispatch_async(concurrentQueue, ^{
        NSLog(@"run task 2");
        sleep(1);
        NSLog(@"complete task 2");
    });
    
    dispatch_barrier_async(concurrentQueue, ^{
        dispatch_async(concurrentQueue, ^{
            NSLog(@"run task 3");
            sleep(1);
            NSLog(@"complete task 3");
        });
    });
    
    dispatch_async(concurrentQueue, ^{
        NSLog(@"run task 4");
        sleep(1);
        NSLog(@"complete task 4");
    });
2020-09-09 15:00:29.198272+0800 Test[8162:786325] run task 2
2020-09-09 15:00:29.198257+0800 Test[8162:786323] run task 1
2020-09-09 15:00:30.198572+0800 Test[8162:786325] complete task 2
2020-09-09 15:00:30.198572+0800 Test[8162:786323] complete task 1
2020-09-09 15:00:30.199012+0800 Test[8162:786323] run task 4
2020-09-09 15:00:30.199018+0800 Test[8162:786325] run task 3
2020-09-09 15:00:31.202372+0800 Test[8162:786325] complete task 3
2020-09-09 15:00:31.202352+0800 Test[8162:786323] complete task 4

3. NSOperation的添加线程依赖addDependency

NSOperationQueue *episodeQueue = [[NSOperationQueue alloc] init];
    
    NSOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"run task 1");
        sleep(1);
        NSLog(@"complete task 1");
    }];
    
    NSOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"run task 2");
        sleep(1);
        NSLog(@"complete task 2");
    }];
    
    NSOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"run task 3");
        sleep(1);
        NSLog(@"complete task 3");
    }];
    
    NSOperation *operationD = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"run task 4");
        sleep(1);
        NSLog(@"complete task 4");
    }];
    
    [operationC addDependency:operationA];
    [operationC addDependency:operationB];
    [operationD addDependency:operationA];
    [operationD addDependency:operationB];
    
    [episodeQueue addOperation:operationA];
    [episodeQueue addOperation:operationB];
    [episodeQueue addOperation:operationC];
    [episodeQueue addOperation:operationD];
2020-09-09 15:16:19.344610+0800 Test[8256:796436] run task 1
2020-09-09 15:16:19.344622+0800 Test[8256:796440] run task 2
2020-09-09 15:16:20.349042+0800 Test[8256:796436] complete task 1
2020-09-09 15:16:20.349069+0800 Test[8256:796440] complete task 2
2020-09-09 15:16:20.349623+0800 Test[8256:796438] run task 4
2020-09-09 15:16:20.349629+0800 Test[8256:796440] run task 3
2020-09-09 15:16:21.354678+0800 Test[8256:796440] complete task 3
2020-09-09 15:16:21.354678+0800 Test[8256:796438] complete task 4
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。