gcd and concurrency programming

29
GCD and Concurrency Programming allenlinli 9.1.13

Upload: allen-lin

Post on 10-May-2015

576 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Gcd and concurrency programming

GCD and Concurrency Programming

allenlinli 9.1.13

Page 2: Gcd and concurrency programming
Page 3: Gcd and concurrency programming

GCD

• Sync vs Async

Page 4: Gcd and concurrency programming

Concurrency Programming

• NSThread

• NSObject

• NSOperation

• Run Loop

• GCD

Page 5: Gcd and concurrency programming

Thread• What is thread

• What is multi-thread

Page 6: Gcd and concurrency programming

Why Multithread on a Single Core?

•Keep UI responsive.

Page 7: Gcd and concurrency programming

NSThread

Page 8: Gcd and concurrency programming

NSThread

• Main thread and other threads

• detachNewThreadSelector:toTarget:withObject:

• Pro: light-weight (for code), directly control

• Cons: Life cycle issue, race condition issue (lock, retain cycle)

Page 9: Gcd and concurrency programming

NSObject

• performSelectorInBackground: withObject:

Page 10: Gcd and concurrency programming

What to use Thread?

• Not in most cases

Page 11: Gcd and concurrency programming

NSThread

Page 12: Gcd and concurrency programming

NSOperation• Pros:

• Add dependency

• setMaxConcurrentOperationCount

• re-use

• Cancel, Suspend

• Priority

• suspend

• compatible with KVO

Page 13: Gcd and concurrency programming

Grand Central Dispatch

Page 14: Gcd and concurrency programming

GCD

• Less memory penalty

• Less configure

• Less manage

• Simplifies the code

Page 15: Gcd and concurrency programming

GCD

• GCD的核⼼心是dispatch queues

• 它管理pools of threads

• 管理queues,⽽而⾮非直接管理threads

It was first released with Mac OS X 10.6, and is also available with iOS 4 and above.

Page 16: Gcd and concurrency programming

Thread pools

• Thread pools.

• Grab a thread from the pool and dispatch the task to it

• Queue the task and wait for a thread to become available.

Page 17: Gcd and concurrency programming

Blocks

!

• Like a function pointer, except it also stores the context the block was created in.

• Beware of retain cycles

void (^aBlock)(int) = ^(int z) {!! NSLog(@“log”);!!};!

Page 18: Gcd and concurrency programming

GCD

• Use queues of blocks rather than threads

Page 19: Gcd and concurrency programming

Dispatch Queue

• Main Queue

• Global Queue

• Serial Queue

Page 20: Gcd and concurrency programming

Dispatch async, sync

• dispatch_async

• dispatch_sync

Page 21: Gcd and concurrency programming

Singleton (for initialisation)

static MyObject *myObject = nil;+(MyObject *)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ if(!myObject){ myObject = [[myObject alloc] init]; } return myObject; }

• dispatch_once

Page 22: Gcd and concurrency programming

Lockless Exclusion

• Serial queue are thread safe inside

-(void)accessSharedVariableAsync:(void(^)(id sharedVariable))block{ // myQueue must be a serial queue dispatch_sync(myQueue, ^{ block([self sharedVariable]); }); }

Page 23: Gcd and concurrency programming

dispatch_group

• dispatch_group_async

• dispatch_group_notify

Page 24: Gcd and concurrency programming

Dispatch

• dispatch_apply

• dispatch_barrier_async

• dispatch_source_create

Page 25: Gcd and concurrency programming

Deadlock

Page 26: Gcd and concurrency programming
Page 27: Gcd and concurrency programming

Q & A

Page 28: Gcd and concurrency programming

Best Sources

• How To Use NSOperations and NSOperationQueues http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

Page 29: Gcd and concurrency programming

• NSOperation vs Grand Central Dispatch

• http://stackoverflow.com/questions/10373331/nsoperation-vs-grand-central-dispatch

• Grand Central Dispatch Design Patterns by Robert Brown

• http://www.slideshare.net/robby_brown/grand-central-dispatch-design-patterns