WWDC2010 Session211 使用Grad Central Dispa

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 22:58 点击:

GCD 概述

1. GCD 包含于 libSystem.dylib

2. 可供所有程序使用.

- #include <dispatch/dispatch.h>

3. GCD API 提供 block-based and function-based variants

- 目前仅提供 block-based API

GCD总结

1. Blocks

- dispatch_async()

2. Queues

- Lightweight list of blocks

- Enqueue/dequeue is FIFO

3. dispatch_get_main_queue()

- Main thread/main runloop

4. dispatch_queue_create()

- Automatic helper thread

GCD的好处

1. 高效 - More CPU cycles available for your code

2. 使用方便

- Blocks are easy to use

- Queues are inherently producer/consumer

3. Systemwide perspective

- Only the OS can balance unrelated subsystems

兼容性

1. Existing threading and synchronization primitives are 100% compatible

2. GCD threads are wrapped POSIX threads

- Do not cancel, exit, kill, join, or detach GCD threads

3. GCD reuses threads

- Restore any per-thread state changed within a block

多线程

Session 206已提到此内容。

锁定资源

1. 对关键资源进行互斥访问。

2. 在线程中按序访问共享资源。

3. Ensure data integrity

没有GCD的代码

- (void)updateImageCacheWithImg:(UIImage*)img {

NSLock *l = self.imageCacheLock;

[l lock];

// Critical section

if ([self.imageCache containsObj:img]) {

[l unlock]; // Don't forget to unlock

return;

}

[self.imageCache addObj:img];

[l unlock];

}

GCD代码

- (void)updateImageCacheWithImg:(NSImage*)img {

dispatch_queue_t queue = self.imageCacheQueue;

dispatch_sync(queue, ^{ //You can change dispatch_sync to dispatch_async to defer critical section

// Critical section

if ([self.imageCache containsObj:img]) {

return;

}

[self.imageCache addObj:img];

});

}

线程间通信

通过以下方法 (Without GCD)

– performSelectorOnMainThread:withObject:waitUntilDone:

– performSelector:onThread:withObject:waitUntilDone:

– performSelector:withObject:afterDelay:

– performSelectorInBackground:withObject:

GCD 代码,等同于 performSelector:onThread:withObject:waitUntilDone:

// waitUntilDone: NO

dispatch_async(queue, ^{

[myObject doSomething:foo withData:bar];

});

// waitUntilDone: YES

dispatch_sync(queue, ^{

[myObject doSomething:foo withData:bar];

});

GCD代码, 等同于 performSelector:withObject:afterDelay:dispatch_time_t delay;delay = dispatch_time(DISPATCH_TIME_NOW, 50000 /* 50μs */);dispatch_after(delay, queue, ^{ [myObject doSomething:foo withData:bar];});GCD代码,等同于 performSelectorInBackground:withObject:dispatch_queue_t queue = dispatch_get_global_queue(0, 0);dispatch_async(queue, ^{ [myObject doSomething:foo withData:bar];});
Global Queues1. Enqueue/dequeue is FIFO


GCD queue 1

2. Concurrent execution- Non-FIFO completion order


GCD queue 2

3. dispatch_get_global_queue(priority, 0)

4. Global queues map GCD activity to real threads 5. Priority bands- DISPAT

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • iphone(object-c)内存管理(1)
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1