GCD and Concurrency
Programming
allenlinli	

9.1.13
GCD
• Sync vs Async
Concurrency
Programming
• NSThread	

• NSObject	

• NSOperation	

• Run Loop	

• GCD
Thread
• What is thread	

• What is multi-thread
Why Multithread on a
Single Core?

•

Keep UI responsive.
NSThread
NSThread
• Main thread and other threads	

• detachNewThreadSelector:toTarget:with
Object:

• Pro: light-weight (for code)...
NSObject

• performSelectorInBackground: withObject:
What to use Thread?
• Not in most cases
NSThread
NSOperation
• Pros:	

• Add dependency	

• setMaxConcurrentOperationCount	

• re-use	

• Cancel, Suspend	

• Priority	

• ...
Grand Central Dispatch
GCD

• Less memory penalty	

• Less configure	

• Less manage	

• Simplifies the code
GCD
• GCD的核⼼心是dispatch queues	

• 它管理pools of threads	

• 管理queues,⽽而⾮非直接管理threads
It was first released with Mac OS X 10.6...
Thread pools
• Thread pools. 	

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


• Queue the task and wait for...
Blocks
void (^aBlock)(int) = ^(int z) {!
!
NSLog(@“log”);!
!
};!

!

• Like a function pointer, except it also stores
the ...
GCD
• Use queues of blocks rather than threads
Dispatch Queue
• Main Queue	

• Global Queue	

• Serial Queue
Dispatch async, sync
• dispatch_async	

• dispatch_sync
Singleton 	

(for initialisation)
• dispatch_once
static MyObject *myObject = nil;

+(MyObject *)sharedInstance
{
static d...
Lockless Exclusion
• Serial queue are thread safe inside
-(void)accessSharedVariableAsync:(void(^)(id sharedVariable))bloc...
dispatch_group
• dispatch_group_async	

• dispatch_group_notify
Dispatch
• dispatch_apply	

• dispatch_barrier_async	

• dispatch_source_create
Deadlock
Q &A
Best Sources
• How To Use NSOperations and

NSOperationQueues http://
www.raywenderlich.com/19788/how-touse-nsoperations-a...
• NSOperation vs Grand Central Dispatch	

• http://stackoverflow.com/questions/10373331/
nsoperation-vs-grand-central-dispa...
Gcd and concurrency programming
Gcd and concurrency programming
Upcoming SlideShare
Loading in...5
×

Gcd and concurrency programming

623

Published on

Published in: Technology, Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
623
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Gcd and concurrency programming

  1. 1. GCD and Concurrency Programming allenlinli 9.1.13
  2. 2. GCD • Sync vs Async
  3. 3. Concurrency Programming • NSThread • NSObject • NSOperation • Run Loop • GCD
  4. 4. Thread • What is thread • What is multi-thread
  5. 5. Why Multithread on a Single Core? • Keep UI responsive.
  6. 6. NSThread
  7. 7. NSThread • Main thread and other threads • detachNewThreadSelector:toTarget:with Object: • Pro: light-weight (for code), directly control • Cons: Life cycle issue, race condition issue (lock, retain cycle)
  8. 8. NSObject • performSelectorInBackground: withObject:
  9. 9. What to use Thread? • Not in most cases
  10. 10. NSThread
  11. 11. NSOperation • Pros: • Add dependency • setMaxConcurrentOperationCount • re-use • Cancel, Suspend • Priority • suspend • compatible with KVO
  12. 12. Grand Central Dispatch
  13. 13. GCD • Less memory penalty • Less configure • Less manage • Simplifies the code
  14. 14. 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.
  15. 15. 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.
  16. 16. Blocks void (^aBlock)(int) = ^(int z) {! ! NSLog(@“log”);! ! };! ! • Like a function pointer, except it also stores the context the block was created in. • Beware of retain cycles
  17. 17. GCD • Use queues of blocks rather than threads
  18. 18. Dispatch Queue • Main Queue • Global Queue • Serial Queue
  19. 19. Dispatch async, sync • dispatch_async • dispatch_sync
  20. 20. Singleton (for initialisation) • dispatch_once static MyObject *myObject = nil;
 +(MyObject *)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ if(!myObject){ myObject = [[myObject alloc] init]; } return myObject; }
  21. 21. 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]); }); }
  22. 22. dispatch_group • dispatch_group_async • dispatch_group_notify
  23. 23. Dispatch • dispatch_apply • dispatch_barrier_async • dispatch_source_create
  24. 24. Deadlock
  25. 25. Q &A
  26. 26. Best Sources • How To Use NSOperations and NSOperationQueues http:// www.raywenderlich.com/19788/how-touse-nsoperations-and-nsoperationqueues
  27. 27. • 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/grandcentral-dispatch-design-patterns
  1. A particular slide catching your eye?

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

×