Showing posts with label GrandCentralDispatch. Show all posts
Showing posts with label GrandCentralDispatch. Show all posts

Friday, April 08, 2011

WWDC2010 Session211 Simplifying iPhone App Development with GCD

GCD Overview

1. GCD is part of libSystem.dylib

2. Available to all Apps.

- #include <dispatch/dispatch.h>

3. GCD API has block-based and function-based variants

- Focus today on block-based API

Introduction to GCD recap

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 Advantages

1. Efficiency - More CPU cycles available for your code

2. Better metaphors

- Blocks are easy to use

- Queues are inherently producer/consumer

3. Systemwide perspective

- Only the OS can balance unrelated subsystems

Compatibility

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

Thursday, April 07, 2011

WWDC2010 Session206 Introducing Blocks and Grand Central Dispatch (2)

Grand Central Dispatch

With GCD, you can make your app responsive. Threading is hard. Using GCD makes it simple and fun. You need not do explicit thread management. Cool!

Keeping your app responsive:

1. Do not block the main thread

2. Move work to another thread

3. Update UI back on main threaad

Code without GCD:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {

  // Controller UI callback on main thread

  DTweet *tw = [[DTweet alloc] initWithMsg:msg];

  [tweets addTweet:tw display:YES];

  tw.img = [imageCache getImgFromURL:url];//bottle neck

  [tweets updateTweet:tw display:YES];

  [tw release];

}

Code with GCD:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {

  // Controller UI callback on main thread

  DTweet *tw = [[DTweet alloc] initWithMsg:msg];

  [tweets addTweet:tw display:YES];

  dispatch_async(image_queue, ^{

    tw.img = [imageCache getImgFromURL:url];

    dispatch_async(main_queue, ^{

      [tweets updateTweet:tw display:YES];

    });

  });

  [tw release];

}

GCD Queues

1. Lightweight list of blocks

2. Enqueue/dequeue is FIFO

3. Enqueue with dispatch_async()

4. Dequeue by automatic thread or main thread

Main Queue

1. Executes blocks one at a time on main thread

2. Cooperates with the UIKit main run loop

3. dispatch_get_main_queue()

Wednesday, April 06, 2011

WWDC2010 Session206 Introducing Blocks and Grand Central Dispatch (1)

Blocks and Grand Central Dispatch are available on iOS4, and have been available on Snow Leopard.

Technology Stack

Technology Stack Block and Grand Central Dispatch  

Blocks

Blacks are available in C++ and Objective-C++.

Basic Blocks

We will use ^ for blocks because this character is unique and could not be used as operator in C++.

Block Literal Syntax

^ [Return type][Arguments] { Body }

Just looks like a function without function name starting with ^. If no return or no arguments, void could be skipped.

Block Syntax

Blocks as Data

We can define Block pointer. It looks like a function pointer: void (*callable)(void);

void (^callable)(void);

This is an ugly block pointer whose argument is also a block pointer.

char *(^worker)(char *a, BOOL(^done)(int));

We can use typedef to simplify it just like what we do for function pointers:

typedef BOOL (^doneBlk_t)(int);

char *(^workB)(char *a, doneBlk_t d);