dispatch API简单总结
来源:互联网 发布:淘宝有客服投诉电话 编辑:程序博客网 时间:2024/04/19 09:00
// 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
- dispatch API简单总结
- Dispatch Queue API 详解
- RN API Keyboard简单总结
- CXF之JAX-WS Dispatch API
- dispatch
- dispatch
- iOS开发之GCD(2)API Dispatch Queue
- iOS开发之GCD(6)API Dispatch Group
- JDK核心API:Java1.5语言新特性简单总结
- 简单vulkan程序的API调用步骤总结
- API总结
- 关于同步和异步的一些总结(dispatch)
- GCD(Grand Central Dispatch)和Block 使用总结
- dispatch源(dispatch source)
- org.pushingpixels.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread
- A Guide to Blocks & Grand Central Dispatch (and the Cocoa API's making use of them)
- 【Tesseract-OCR】在VS2010环境下调用API方法---简单快速之总结
- 【Tesseract-OCR】在VS2010环境下调用API方法—简单快速之总结
- iOS开发网络篇—发送json数据给服务器以及多值参数
- oracle 链接使用
- 组播技术
- Effective STL 学习笔记
- iOS开发网络篇—数据缓存
- dispatch API简单总结
- 8.15作业
- Java Semaphore 信号量
- netty学习之二:ByteBuf解读
- Android事件处理
- iOS开发网络篇—使用ASI框架进行文件下载
- maven repository
- horizontalscrollview
- Android优化笔记