0
BlocksGrand Central Dispatch
Blocks / GCD• Objective-C 2.1• Snow Leopard• iOS 4
Blocks• lambda, closure• Lisp, SmallTalk, Ruby• C, C++, Objective-C
^
DEMO
Block• Blocks Objective-C• Block •           copy •      release        Block
Block• Blocks           retain•      Block                    __block
Blocks vs Function Pointer  •   vs  •  •
Grand Central Dispatch••••   Blocks
GCD
GCD
GCD
GCD Queue• Main dispatch queue• Concurrent queue• Serial queue
Main dispatch Queue
Concurrent Queue
Serial Queue
GCD Group• dispatch_group_create• dispatch_group_async• dispatch_group_wait• Thread join
GCD Sources•    •• Event handler   GCD Queue
GCD Source
DEMO
•    • Introducing Blocks and Grand Central Dispatch    • Concurrency Programming Guide    • Grand Central Dispatch (GCD) ...
Upcoming SlideShare
Loading in...5
×

Blocks & Grand Central Dispatch

2,120

Published on

Blocks & Grand Central Dispatch on 2010 Cocoaheads Beijing

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,120
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

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 of "Blocks & Grand Central Dispatch"

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

      Clipping is a handy way to collect important slides you want to go back to later.

    ×