• Like
Blocks & Grand Central Dispatch
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Blocks & Grand Central Dispatch

  • 2,073 views
Published

Blocks & Grand Central Dispatch on 2010 Cocoaheads Beijing

Blocks & Grand Central Dispatch on 2010 Cocoaheads Beijing

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,073
On SlideShare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
23
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • Blocks和GCD作为Objective-C和Cocoa的特性,最早在Mac OS X 10.6 Snow Leopard引入。iPhone从iOS 4开始支持。主要用于解决异步问题。\n
  • Blocks类似Lamdba演算和closure,是一段函数体,可以作为对象传递,又能保留其词法作用域。\n\n大量运用于函数式语言和很多动态语言中,比如Lisp, SmallTalk和Ruby。和Ruby中对此类对象的称呼相同。\nBlocks是Apple为C/C++/Objective-C提供的非标准扩展。在GCC和Clang LLVM中分别实现。\n\n将函数的某一部分以另一种方式执行。比如异步执行,或者在另一个作用域中执行。\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 线程开发相对复杂。开多少线程合适?线程调度和管理。线程执行的顺序。竞争机制。\nGCD利用队列分发的方式,提供了比较透明的线程管理,简单易用。开发者不用考虑到底要开多少线程。\n
  • \n
  • 开发者向queue中加入Task,无需知道到底分配了多少线程。\ntask以先进先出的方式执行,系统自行管理线程。线程所占用的内存是系统层而不是应用层的。\n这是一个并发队列。 如果是一个串行队列,一次只执行一个task,可以保证task的执行顺序,无需使用锁机制。\n
  • \n
  • \n
  • Main dispatch queue是一个全局串行队列,在应用的主run loop中。\n
  • \n
  • 串行队列,一个队列每次只执行一个task,可以创建多个队列,不同队列之间并发运行。如果task需要同步访问某一个资源,可以采用串行队列来解决同步问题,而不用加锁。\n\n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. BlocksGrand Central Dispatch
  • 2. Blocks / GCD• Objective-C 2.1• Snow Leopard• iOS 4
  • 3. Blocks• lambda, closure• Lisp, SmallTalk, Ruby• C, C++, Objective-C
  • 4. ^
  • 5. DEMO
  • 6. Block• Blocks Objective-C• Block • copy • release Block
  • 7. Block• Blocks retain• Block __block
  • 8. Blocks vs Function Pointer • vs • •
  • 9. Grand Central Dispatch•••• Blocks
  • 10. GCD
  • 11. GCD
  • 12. GCD
  • 13. GCD Queue• Main dispatch queue• Concurrent queue• Serial queue
  • 14. Main dispatch Queue
  • 15. Concurrent Queue
  • 16. Serial Queue
  • 17. GCD Group• dispatch_group_create• dispatch_group_async• dispatch_group_wait• Thread join
  • 18. GCD Sources• •• Event handler GCD Queue
  • 19. GCD Source
  • 20. DEMO
  • 21. • • Introducing Blocks and Grand Central Dispatch • Concurrency Programming Guide • Grand Central Dispatch (GCD) Reference• WWDC 2010: Session 206, 211