dispatch API简单总结

来源:互联网 发布:淘宝有客服投诉电话 编辑:程序博客网 时间:2022/08/11 17:14


// dispatch API- (void)dispatch_demo{    //生成Serial Dispatch Queue 时,传入NULL(DISPATCH_QUEUE_SERIAL)    //Concurrent Dispatch Queue时,传入 DISPATCH_QUEUE_CONCURRENT    dispatch_queue_t mySerialDispatchQueue = dispatch_queue_create("com.example.gcd.mySerialDispatchQueue", NULL);        //Main Dispatch Queue / Global Dispatch Queue    dispatch_queue_t mainQueue = dispatch_get_main_queue();    dispatch_queue_t globalQueueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);    dispatch_queue_t globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_queue_t globalQueueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);    dispatch_queue_t globalQueueBackground = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);        //dispatch_set_target_queue    dispatch_set_target_queue(mySerialDispatchQueue, globalQueueBackground);        //dispatch_after    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        NSLog(@"waited at lease one seconds");    });        //dispatch group    dispatch_group_t group = dispatch_group_create();    dispatch_group_async(group, globalQueueDefault, ^{        NSLog(@"blk0");    });        dispatch_group_async(group, globalQueueDefault, ^{        NSLog(@"blk1");    });        //dispatch_group_notify    dispatch_group_notify(group, mainQueue, ^{        NSLog(@"done");    });        //dispatch_group_wait, DISPATCH_TIME_NOW/DISPATCH_TIME_FOREVER    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);        //dispatch_barrier_async    dispatch_block_t blk0_for_reading = ^{NSLog(@"blk0_for_reading");};    dispatch_block_t blk1_for_reading = ^{NSLog(@"blk1_for_reading");};    dispatch_block_t blk2_for_reading = ^{NSLog(@"blk2_for_reading");};    dispatch_block_t blk_for_writing = ^{NSLog(@"blk_for_writing");};    dispatch_block_t blk3_for_reading = ^{NSLog(@"blk3_for_reading");};    dispatch_block_t blk4_for_reading = ^{NSLog(@"blk4_for_reading");};        dispatch_async(globalQueueDefault, blk0_for_reading);    dispatch_async(globalQueueDefault, blk1_for_reading);    dispatch_async(globalQueueDefault, blk2_for_reading);    dispatch_barrier_async(globalQueueDefault, blk_for_writing);    dispatch_async(globalQueueDefault, blk3_for_reading);    dispatch_async(globalQueueDefault, blk4_for_reading);        //dispatch_sync    dispatch_sync(globalQueueDefault, ^{        NSLog(@"dispatch_sync");    });        //dispatch_apply, 4,3,1,2,0, done    dispatch_apply(5, globalQueueDefault, ^(size_t index) {        NSLog(@"index = %zu", index);    });        //dispatch_suspend / dispatch_resume    dispatch_suspend(mySerialDispatchQueue);    dispatch_resume(mySerialDispatchQueue);        //dispatch_semaphore    dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);        dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);    //等待信号    long result = dispatch_semaphore_wait(semaphore, time);    if (0 == result) {        // 控制相关处理    } else {        //other handler    }        //在其他地方调用 dispatch_semaphore_signal(semaphore);            //dispatch_once    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        NSLog(@"once create");    });        //dispatch I/O    int fd = 0; // fd 初始化,例如打开文件或者socket    dispatch_queue_t pipe_q = dispatch_queue_create("PipeQ", 0);    dispatch_io_t pipe_channel = dispatch_io_create(DISPATCH_IO_STREAM, fd, pipe_q, ^(int error) {        close(fd);    });        dispatch_data_t pipedata; //初始化    //    *out_fd = 0;//fdpair[1]; out 初始化    dispatch_io_set_low_water(pipe_channel, SIZE_MAX);        dispatch_io_read(pipe_channel, 0, SIZE_MAX, pipe_q, ^(bool done, dispatch_data_t data, int error) {        if (0 == error)        {            size_t len = dispatch_data_get_size(pipedata);                        if (len > 0)            {                const char *bytes = NULL;                char *encoded;                                dispatch_data_t md = dispatch_data_create_map(pipedata, &bytes, &len);//                encoded = asl_core_encode_buffer(bytes, len);                //handle ...            }        }        else        {                    }    });}





0 0
原创粉丝点击