GCD and Concurrency
Programming
allenlinli	

9.1.13
Gcd and concurrency programming
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), directly control	

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

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

• Add dependency	

• setMaxConcurrentOperationCount	

• re-use	

• Cancel, Suspend	

• Priority	

• suspend	

• compatible with KVO
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, and is also
available with iOS 4 and above.
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.
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
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 dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if(!myObject){
myObject = [[myObject alloc] init];
}
return myObject;
}
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]);
});
}
dispatch_group
• dispatch_group_async	

• dispatch_group_notify
Dispatch
• dispatch_apply	

• dispatch_barrier_async	

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

NSOperationQueues http://
www.raywenderlich.com/19788/how-touse-nsoperations-and-nsoperationqueues
• 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

More Related Content

PPTX
Introduction to Napa.js
PPTX
Awsugnz re invent recap infra
PDF
Grand Central Dispatch and multi-threading [iCONdev 2014]
KEY
Concurrent Programming Using the Disruptor
KEY
Blocks & Grand Central Dispatch
PDF
Tugrik: A new persistence option for Pharo
PDF
Cinder Status Openstack Shanghai
PDF
Kubernetes on bare metal after a year in production - Michal Ambroziewicz
Introduction to Napa.js
Awsugnz re invent recap infra
Grand Central Dispatch and multi-threading [iCONdev 2014]
Concurrent Programming Using the Disruptor
Blocks & Grand Central Dispatch
Tugrik: A new persistence option for Pharo
Cinder Status Openstack Shanghai
Kubernetes on bare metal after a year in production - Michal Ambroziewicz

What's hot (20)

PDF
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
PPTX
NSBCon UK nservicebus on Azure by Yves Goeleven
PPTX
Amazon Cloud Overview
PDF
Pharo Update
PPSX
LMAX Disruptor - High Performance Inter-Thread Messaging Library
PDF
Virtualizing MongoDB: Cloud, EC2, OpenStack, VMs...or Dedicated?
PPTX
Meteor presentation
PDF
Nick Fisk - low latency Ceph
PDF
JBCNConf: jBPM & Vert.x Reactive and Polyglot BPM
PPTX
Containerization - The DevOps Revolution
PDF
Intro to GemStone/S
PPTX
Node.js code tracing
PPTX
Introduction to node
PDF
An introduction to node3
PPT
Building your own NSQL store
PDF
remodel your persistence layer
PDF
PDF
Node
PDF
Ndb cluster 80_use_cases
PDF
reInvent 2021 Recap and k9s review
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
NSBCon UK nservicebus on Azure by Yves Goeleven
Amazon Cloud Overview
Pharo Update
LMAX Disruptor - High Performance Inter-Thread Messaging Library
Virtualizing MongoDB: Cloud, EC2, OpenStack, VMs...or Dedicated?
Meteor presentation
Nick Fisk - low latency Ceph
JBCNConf: jBPM & Vert.x Reactive and Polyglot BPM
Containerization - The DevOps Revolution
Intro to GemStone/S
Node.js code tracing
Introduction to node
An introduction to node3
Building your own NSQL store
remodel your persistence layer
Node
Ndb cluster 80_use_cases
reInvent 2021 Recap and k9s review
Ad

Similar to Gcd and concurrency programming (20)

PPTX
PDF
Concurrent Programming in iOS
PDF
kranonit S06E01 Игорь Цинько: High load
PDF
Multithreading and Parallelism on iOS [MobOS 2013]
ODP
Concept of thread
PDF
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
PPTX
Multi core programming 2
PPTX
NodeJS - Server Side JS
PPT
18_Node.js.ppt
PPT
002 - Introduction to CUDA Programming_1.ppt
PPTX
Introduction to node.js
PDF
GPU performance analysis
PPT
18_Node.js.ppt
PDF
DX12 & Vulkan: Dawn of a New Generation of Graphics APIs
PPTX
Clr jvm implementation differences
PDF
GPU architecture notes game prog gpu-arch.pdf
PPTX
Cuda Architecture
PPTX
Graphics processing uni computer archiecture
PDF
IWOCL 2025 Write Once, Deploy Many – 3D Rendering With SYCL Cross-Vendor Supp...
PDF
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrent Programming in iOS
kranonit S06E01 Игорь Цинько: High load
Multithreading and Parallelism on iOS [MobOS 2013]
Concept of thread
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
Multi core programming 2
NodeJS - Server Side JS
18_Node.js.ppt
002 - Introduction to CUDA Programming_1.ppt
Introduction to node.js
GPU performance analysis
18_Node.js.ppt
DX12 & Vulkan: Dawn of a New Generation of Graphics APIs
Clr jvm implementation differences
GPU architecture notes game prog gpu-arch.pdf
Cuda Architecture
Graphics processing uni computer archiecture
IWOCL 2025 Write Once, Deploy Many – 3D Rendering With SYCL Cross-Vendor Supp...
Concurrency and Multithreading Demistified - Reversim Summit 2014
Ad

More from Li Lin (8)

PDF
Kaizen - The Improvement Process. Allen Lin 2020.7.20 3.pdf
PDF
SharePlay 2022.7.15 Allen Lin.pdf
PDF
Cocoahead index identifier
PPTX
Mind Map
PDF
An Example-Driven API Tester
PDF
Multiple Connectivity Framework Keynote
PDF
Autolayout keynote
PDF
Core Bluetooth and BLE 101
Kaizen - The Improvement Process. Allen Lin 2020.7.20 3.pdf
SharePlay 2022.7.15 Allen Lin.pdf
Cocoahead index identifier
Mind Map
An Example-Driven API Tester
Multiple Connectivity Framework Keynote
Autolayout keynote
Core Bluetooth and BLE 101

Recently uploaded (20)

PPTX
Configure Apache Mutual Authentication
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PDF
Comparative analysis of machine learning models for fake news detection in so...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
Five Habits of High-Impact Board Members
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Modernising the Digital Integration Hub
PPT
Geologic Time for studying geology for geologist
Configure Apache Mutual Authentication
A proposed approach for plagiarism detection in Myanmar Unicode text
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Microsoft Excel 365/2024 Beginner's training
Taming the Chaos: How to Turn Unstructured Data into Decisions
Developing a website for English-speaking practice to English as a foreign la...
A review of recent deep learning applications in wood surface defect identifi...
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Comparative analysis of machine learning models for fake news detection in so...
sustainability-14-14877-v2.pddhzftheheeeee
CloudStack 4.21: First Look Webinar slides
Convolutional neural network based encoder-decoder for efficient real-time ob...
Five Habits of High-Impact Board Members
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
UiPath Agentic Automation session 1: RPA to Agents
NewMind AI Weekly Chronicles – August ’25 Week III
Getting started with AI Agents and Multi-Agent Systems
Modernising the Digital Integration Hub
Geologic Time for studying geology for geologist

Gcd and concurrency programming