WWDC2010 Session211 使用Grad Central Dispatch简化iPhone开发
来源:互联网 发布:神圣计划软件 编辑:程序博客网 时间:2024/05/16 03:22
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
2. Concurrent execution- Non-FIFO completion order
3. dispatch_get_global_queue(priority, 0)
4. Global queues map GCD activity to real threads 5. Priority bands- DISPATCH_QUEUE_PRIORITY_HIGH - DISPATCH_QUEUE_PRIORITY_DEFAULT - DISPATCH_QUEUE_PRIORITY_LOW
下面三节讲述了很多内容,但我还没有理解。如有兴趣,请看原始视频。- Dispatch Sources
- Source Cancellation- Target Queues
GCD对象
GCD方法大全
管理对象的生命周期
1. Ensure objects captured by blocks are valid when blocks are executed
- Objects must be retained and released around asynchronous operations
2. Objective-C objects captured by blocks are auto-retained and auto-released
3. Other objects captured by blocks must be retained by your code
- CFRetain()/CFRelease()
- dispatch_retain()/dispatch_release()
挂起和恢复
1. Suspend and resume only affects queues and sources you create
- Sources are created suspended
2. Suspension is asynchronous
- Takes effect between blocks
3. Your queues can predictably suspend objects that target them
程序上下文
1. Applications can attach custom data to GCD objects
- dispatch_set_context()/dispatch_get_context()
2. Optional finalizer callback
- dispatch_set_finalizer_f()
- Allows attached context to be freed with object
- Called on the target queue of the object
WWDC2010 Session211 Simplifying iPhone App Development with Grand Central Dispatch
Daniel Steffen - Core OS
- WWDC2010 Session211 使用Grad Central Dispatch简化iPhone开发
- WWDC2010 Session211 使用Grad Central Dispatch简化iPhone开发
- Grad Central Dispatch
- WWDC2010 Session206 介绍iPhone上的Blocks和Grand Central Dispatch (1)
- WWDC2010 Session206 介绍iPhone上的Blocks和Grand Central Dispatch (2)
- WWDC2010 Session206 介绍iPhone上的Blocks和Grand Central Dispatch
- WWDC2010 Session206 介绍iPhone上的Blocks和Grand Central Dispatch (2)
- iOS开发之-Grand Central Dispatch
- Grand Central Dispatch(GCD)介绍和使用
- iOS多线程编程Crand Central Dispatch使用
- Grand Central Dispatch(GCD)介绍和使用
- iphone编程dispatch的使用
- iOS线程开发之--BLOCK & GCD(Grand Central Dispatch)
- iOS线程开发之--BLOCK & GCD(Grand Central Dispatch)
- iOS线程开发之--BLOCK & GCD(Grand Central Dispatch)
- iOS多线程开发(五)---GCD(Grand Central Dispatch)
- iOS开发系列--Grand Central Dispatch 基础教程:Part 1/2
- iOS多线程开发系列之(三)Grand Central Dispatch(GCD)
- WWDC2010 Session206 介绍iPhone上的Blocks和Grand Central Dispatch (1)
- Linux Shell简介
- 迷宫问题
- WWDC2010 Session206 介绍iPhone上的Blocks和Grand Central Dispatch (2)
- iphone学习(原创+搜集)
- WWDC2010 Session211 使用Grad Central Dispatch简化iPhone开发
- 解决:对COM 组件的调用返回了错误 HRESULT E_FAIL
- 《objective-c 2.0 程序设计》学习笔记
- 让支持Block的app运行于iOS3.x
- 深入浅出 Android Service(1)
- 如何做1像素细边框的table?
- django的helloworld程序
- 在android应用中植入Admob广告赚钱
- SQLite在VC下的使用