SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
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), 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
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

Contenu connexe

Tendances

Virtualizing MongoDB: Cloud, EC2, OpenStack, VMs...or Dedicated?
Virtualizing MongoDB: Cloud, EC2, OpenStack, VMs...or Dedicated?Virtualizing MongoDB: Cloud, EC2, OpenStack, VMs...or Dedicated?
Virtualizing MongoDB: Cloud, EC2, OpenStack, VMs...or Dedicated?
MongoDB
 
Containerization - The DevOps Revolution
Containerization - The DevOps RevolutionContainerization - The DevOps Revolution
Containerization - The DevOps Revolution
Yulian Slobodyan
 

Tendances (20)

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

Similaire à Gcd and concurrency programming

kranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High loadkranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High load
Krivoy Rog IT Community
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
Kuba Břečka
 
002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt
ceyifo9332
 
Graphics processing uni computer archiecture
Graphics processing uni computer archiectureGraphics processing uni computer archiecture
Graphics processing uni computer archiecture
Haris456
 
T4T Training day - NodeJS
T4T Training day - NodeJST4T Training day - NodeJS
T4T Training day - NodeJS
Tim Sommer
 

Similaire à Gcd and concurrency programming (20)

Cuda
CudaCuda
Cuda
 
Concurrent Programming in iOS
Concurrent Programming in iOSConcurrent Programming in iOS
Concurrent Programming in iOS
 
kranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High loadkranonit S06E01 Игорь Цинько: High load
kranonit S06E01 Игорь Цинько: High load
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
How we got to 1 millisecond latency in 99% under repair, compaction, and flus...
 
Multi core programming 2
Multi core programming 2Multi core programming 2
Multi core programming 2
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS
 
18_Node.js.ppt
18_Node.js.ppt18_Node.js.ppt
18_Node.js.ppt
 
002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
GPU performance analysis
GPU performance analysisGPU performance analysis
GPU performance analysis
 
18_Node.js.ppt
18_Node.js.ppt18_Node.js.ppt
18_Node.js.ppt
 
DX12 & Vulkan: Dawn of a New Generation of Graphics APIs
DX12 & Vulkan: Dawn of a New Generation of Graphics APIsDX12 & Vulkan: Dawn of a New Generation of Graphics APIs
DX12 & Vulkan: Dawn of a New Generation of Graphics APIs
 
Clr jvm implementation differences
Clr jvm implementation differencesClr jvm implementation differences
Clr jvm implementation differences
 
Cuda Architecture
Cuda ArchitectureCuda Architecture
Cuda Architecture
 
Graphics processing uni computer archiecture
Graphics processing uni computer archiectureGraphics processing uni computer archiecture
Graphics processing uni computer archiecture
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
 
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
 
T4T Training day - NodeJS
T4T Training day - NodeJST4T Training day - NodeJS
T4T Training day - NodeJS
 

Plus de Li Lin (8)

Kaizen - The Improvement Process. Allen Lin 2020.7.20 3.pdf
Kaizen - The Improvement Process. Allen Lin 2020.7.20 3.pdfKaizen - The Improvement Process. Allen Lin 2020.7.20 3.pdf
Kaizen - The Improvement Process. Allen Lin 2020.7.20 3.pdf
 
SharePlay 2022.7.15 Allen Lin.pdf
SharePlay 2022.7.15 Allen Lin.pdfSharePlay 2022.7.15 Allen Lin.pdf
SharePlay 2022.7.15 Allen Lin.pdf
 
Cocoahead index identifier
Cocoahead index identifierCocoahead index identifier
Cocoahead index identifier
 
Mind Map
Mind MapMind Map
Mind Map
 
An Example-Driven API Tester
An Example-Driven API TesterAn Example-Driven API Tester
An Example-Driven API Tester
 
Multiple Connectivity Framework Keynote
Multiple Connectivity Framework KeynoteMultiple Connectivity Framework Keynote
Multiple Connectivity Framework Keynote
 
Autolayout keynote
Autolayout keynoteAutolayout keynote
Autolayout keynote
 
Core Bluetooth and BLE 101
Core Bluetooth and BLE 101Core Bluetooth and BLE 101
Core Bluetooth and BLE 101
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Dernier (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

Gcd and concurrency programming