iOS多线程(三)

任务1 2 子线程执行完毕后在执行任务3

//
//  ViewController.m
//  线程
//
//  Created by Evan on 2021/8/15.
//

#import "ViewController.h"

///任务 1 2执行完毕后 在执行任务3
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self gcdGroup];
    [self gcdbarrier];
//    [self semaphore];
}
//实现方式1 dispatch_group_t
- (void)gcdGroup {
    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, queue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task1 thread%@----%d",[NSThread currentThread],i);
        }
    });
    
    dispatch_group_async(group, queue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task2 thread%@----%d",[NSThread currentThread],i);
        }
    });
    
    dispatch_group_notify(group, mainQueue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task3 thread%@----%d",[NSThread currentThread],i);
        }
    });
}

/// 方式二 栅栏函数
- (void)gcdbarrier {
    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task1 thread%@----%d",[NSThread currentThread],i);
        }
    });
    dispatch_async(queue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task2 thread%@----%d",[NSThread currentThread],i);
        }
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"dispatch_barrier_async");
    });
    dispatch_async(mainQueue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task3 thread%@----%d",[NSThread currentThread],i);
        }
    });
}

//实现方式3 dispatch_semaphore_t
- (void)semaphore {
    
    //1. dispatch_semaphore_create => 创建一个信号量的初始值
    //传入的参数value必须大于或等于0,否则dispatch_semaphore_create会返回NULL。
   dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);//dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    dispatch_async(queue, ^{
        for (int i=0; i<6; i++) {
            sleep(1);
            NSLog(@"task1 thread%@----%d",[NSThread currentThread],i);
        }
//        dispatch_semaphore_signal => 发送一个信号
//        这个函数会使传入的信号量dsema的值加1。
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_async(queue, ^{
        for (int i=0; i<6; i++) {
            sleep(1);
            NSLog(@"task2 thread%@----%d",[NSThread currentThread],i);
        }
        dispatch_semaphore_signal(semaphore);
    });
//    dispatch_semaphore_wait => 等待一个信号
//    这个函数会使传入的信号量dsema的值减1;
//    如果dsema信号量的值大于0,该函数所处线程就继续执行下面的语句,并且将信号量的值减1;
//    如果desema的值为0,那么这个函数就阻塞当前线程等待timeout(注意timeout的类型为dispatch_time_t,
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//    timeout
//    在设置timeout时,比较有用的两个宏:
//    DISPATCH_TIME_NOW     表示当前;
//    DISPATCH_TIME_FOREVER   表示未来;
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

    dispatch_async(mainQueue, ^{
        for (int i=0; i<6; i++) {
            NSLog(@"task3 thread%@----%d",[NSThread currentThread],i);
        }
    });
    
    
}

- (void)NSOperationQueue {
//    添加依赖
}



@end

你可能感兴趣的:(iOS多线程(三))